import Foundation↵
↵
struct Post: Decodable, Identifiable {↵
let id: Int↵
let title: String↵
}↵
↵
func loadPosts() async throws -> [Post] {↵
let url = URL(string: "https://example.com/posts.json")!↵
let (data, response) = try await URLSession.shared.data(from: url)↵
guard (response as? HTTPURLResponse)?.statusCode == 200 else {↵
throw URLError(.badServerResponse)↵
}↵
return try JSONDecoder().decode([Post].self, from: data)↵
}
Click here or press any key to type