UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 60 Challenge

Forums > 100 Days of SwiftUI

Hello,

the fetched data won't show on the screen, also I don't get any error. What I have done wrong?

Thanks

import SwiftUI

struct ContentView: View {
    @State private var users = [User]()

    var body: some View {
        NavigationView {
            List(users) { user in
                NavigationLink {
                    DetailView()
                } label: {
                    VStack(alignment: .leading) {
                        Text(user.name)
                            .font(.headline)
                        Text(user.isActive ? "Active" : "Offline")
                            .foregroundColor(user.isActive ? .green : .red)
                            .font(.subheadline)
                    }
                }
            }
            .navigationTitle("Friendface")
            .task {
                await loadData()
            }
        }
    }

    func loadData() async {
        if users.isEmpty {
            guard let url = URL(string: "https://www.hackingwithswift.com/samples/friendface.json") else {
                print("Invalid URL")
                return
            }

            do {
                let (data, _) = try await URLSession.shared.data(from: url)

                JSONDecoder().dateDecodingStrategy = .iso8601

                if let decodedResponse = try? JSONDecoder().decode([User].self, from: data) {
                    await MainActor.run {
                        users = decodedResponse
                    }
                }
            } catch {
                print("Invalid data")
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
import Foundation

struct User: Codable, Identifiable {
    let id: UUID
    var isActive: Bool
    let name: String
    let age: Int
    let company: String
    let email: String
    let address: String

    let about: String
    let registered: Date
    let tags: [String]

    let friends: [Friend]
}
import Foundation

struct Friend: Codable, Identifiable {
    let id: UUID
    let name: String
}

2      

You haven't created an instance of JSONDecoder, just used it and discarded the result.

JSONDecoder().dateDecodingStrategy = .iso8601

if let decodedResponse = try? JSONDecoder().decode([User].self, from: data) {
    await MainActor.run {
        users = decodedResponse
    }
}

You have to create an instance of JSONDecoder, and use that instead.

Also you possibly need the optional try?.

let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601

users = try decoder.decode([User].self, from: data) 

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.