Skip to content

Instantly share code, notes, and snippets.

@rintoandrews90
Created April 10, 2024 17:17
Show Gist options
  • Save rintoandrews90/9bbd066643eb9a56d0e1ca11f8cee326 to your computer and use it in GitHub Desktop.
Save rintoandrews90/9bbd066643eb9a56d0e1ca11f8cee326 to your computer and use it in GitHub Desktop.
struct TaskExample: View {
@State private var source = ""
var body: some View {
ScrollView {
Text(source)
}
.task {
await fetchSource()
}
}
func fetchSource() async {
do {
let url = URL(string: "https://hws.dev/inbox.json")!
let (data, _) = try await URLSession.shared.data(from: url)
source = String(decoding: data, as: UTF8.self)
} catch {
source = " Failed to fetch apple.com"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment