The following code fails on Swift Playgrounds for Mac but not Swift Playgrounds for iPad:
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 }
Repost of https://developer.apple.com/forums/thread/708856 which I think was incorrectly accepted as answered. Example from https://www.hackingwithswift.com/books/ios-swiftui/sending-and-receiving-codable-data-with-urlsession-and-swiftui