Nudging the thread… is this a bug or am I missing something?
I misread your previous post. Here is a reproducible example, start with File > New App and then a simple app using a network call:
import SwiftUI
struct Response: Codable {
var results: [Result]
}
struct Result: Codable {
var trackId: Int
var trackName: String
var collectionName: String
}
struct ContentView: View {
@State private var results = [Result]()
var body: some View {
List(results, id: \.trackId) { item in
VStack(alignment: .leading) {
Text(item.trackName)
.font(.headline)
Text(item.collectionName)
}
}
.task {
results = await loadData()
}
}
}
func loadData() async -> [Result]{
var results = [Result(trackId: -1, trackName: "No Tracks Available", collectionName: "No Collection Available")]
let url = URL(string: "https://itunes.apple.com/search?term=taylor+swift&entity=song")
do {
let (data, _) = try await URLSession.shared.data(from: url!)
if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
results = decodedResponse.results
}
} catch {
print("Invalid data")
}
return results
}
For which I saw:
Seems like I might have a wrong setting or something? I have tried this on two separate macbooks with no success on either.
I started from:
https://www.hackingwithswift.com/forums/swiftui/urlsession-shared-data-from-url-fails-in-swift-playgrounds-for-mac/14987
in the playground, it is clear to me where
needsIndefiniteExecution = true
goes, simply at the top of the script. In the app I am making, I don’t know where it goes. When I put it at the top of the file, it complains about not being able to run in a global scope. Is there a difference between the playground and the app (made in Swift playgrounds)?