Demo.swift
ContentView.swift
App.swift
import SwiftUI
import Foundation
struct Todo: Decodable, Identifiable { let id: Int; let title: String }
func fetchTodos() async throws -> [Todo] {
let url = URL(string: "https://jsonplaceholder.typicode.com/todos?_limit=2")!
let (data, _) = try await URLSession.shared.data(from: url)
return try JSONDecoder().decode([Todo].self, from: data)
}
struct NetworkingGetDemo: View {
@State private var todos: [Todo] = []
var body: some View {
List(todos) { t in Text(t.title) }
.task {
do { todos = try await fetchTodos() } catch { print(error) }
}
}
}
import SwiftUI
struct ContentView: View {
var body: some View {
NetworkingGetDemo()
}
}
import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup { ContentView() }
}
}