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

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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.