TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED -- Day 60 Milestone Question

Forums > 100 Days of SwiftUI

Hello I have the following code which is returning a blank screen other than the temporary navigation title, any help would be appreciated:

import SwiftUI

struct User: Codable{
    struct Friend: Codable {
        let id: String
        let name: String
    }

    var id : String
    var isActive: Bool
    var name: String
    var age: Int
    var company: String
    var email: String
    var address: String
    var about: String
    var registered: Date = Date()
    var tags: [String] = []
    var friends: [Friend] = []
}

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

    var body: some View {
        NavigationStack {
            List(users, id: \.id) { user in
                VStack {
                    Text("User name: \(user.name)")
                }   // End VStack
            }   // End List
            .navigationTitle("Test Data")
            .task {
                await loadData()    // We attempt to load our data from the URL
            }
        }
    }   // end var body

    // Async function that retrieves data from the internet:
    func loadData() async {
        //  1. Create the URL we want to read:
        guard let url = URL(string: "https://www.hackingwithswift.com/samples/friendface.json")
        else {
            print("Invalid URL")
            return
        }

        //  2. Attempty to fetch data from URL:
        do {
            let(data, _) = try await URLSession.shared.data(from: url)
            //  3. If successful then we decode the result using the Response struct and then put the resulting decoded data into the results array of Users:
            if let decodedResponse = try? JSONDecoder().decode([User].self, from: data) {
                users = decodedResponse
            }
        } catch {
            print("Invalid data")
        }
    }   // End loadData() func
}   // End ContentView struct

#Preview {
    ContentView()
}

2      

I made the following changes inside my loadData() function and the data started loading up:

func loadData() async {
    //  1. Create the URL we want to read:
    guard let url = URL(string: "https://www.hackingwithswift.com/samples/friendface.json")
    else {
        print("Invalid URL")
        return
    }

    //  2. Attempty to fetch data from URL:
    let decoder = JSONDecoder()
    decoder.dateDecodingStrategy = .iso8601

    do {
        let(data, _) = try await URLSession.shared.data(from: url)
        //  3. If successful then we decode the result using the Response struct and then put the resulting decoded data into the results array of Users:
        if let decodedResponse = try? decoder.decode([User].self, from: data) {   // [User] is how the data is formatted in the JSON file, in this case we have an array of User in the JSON file
            users = decodedResponse
        }
    } catch {
        print("Invalid data")
    }
}   // End loadData() func

Seems like what was missing was that dateDecodingStrategy :/

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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

Reply to this topic…

You need to create an account or log in to reply.

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.