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

SOLVED: SwiftUI, iOS15 Day 60 Challenge: retrieving data and loading to list

Forums > SwiftUI

Hi, I'm not getting my list, and I cannot work out why not as it does not appear to be showing me any errors:

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

struct User: Identifiable, Codable {

    let id: UUID
    var isActive: Bool
    let name: String
    var age: Int
    var company: String
    var email: String
    var address: String
    var about: String
    var registered: Date
    var tags: [String]
    var friends: [Friend]

}

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

    func loadData() async {
        guard !users.isEmpty else {
            return
        }

        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)

            if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
                users = decodedResponse.users

            }
        } catch {
            print("Invalid data")
        }

    }

    var body: some View {
        NavigationView {
            List(users) {user in
                HStack {
                    Text(user.name)
                        //.font(.body)

                    Text(user.isActive ? "Active" : "Unavailable")
                        //.font(.headline)
                }
            }
            .task {
                await loadData()
            }
            .navigationTitle("FriendsApp")

        }
    }
}

Help appreciated! Thank you.

3      

Change this:

if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
    users = decodedResponse.users
}

to this:

do {
    let decodedResponse = try JSONDecoder().decode(Response.self, from: data)
    users = decodedResponse.users
} catch {
    print(error)
}

and you'll actually get errors so you can start figuring out what's wrong. Using try? converts any error into nil and swallows it up so that you don't see it.

3      

@roosterboy, thanks for your help.

I ended up getting rid of the Response struct, and adding in the date decoding strategy (which I had interpreted in the text that it was so common it was just done automatically, and not that there was the .iso8601 that you could use), and using:

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

3      

Hi @DantonCorbel! I have the same problem and I'm not getting my list. I think it is for the date decoding strategy but I can't implement this code

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

I don't know where to put it. Any help? Thanks!

2      

  1. @CacereLucas it has been a while since I did this - but I replaced the 'if let' in my 'ContentView: View' struct, in the 'loadData()' function. Any help?

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.