NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

During 49: Caught a "sleeping guard" at the url check?

Forums > 100 Days of SwiftUI

UPDATE ON CONSOLE: It turns out I had to go to View > Debug > Activate Console to get the durned thing working again! (Why it stopped working in the first place is anybody's guess...)

The original question, if anyone is curious, is posted in the comments below:

struct Result: Codable {
    //these are some of the properties sent back from iTunes
    var trackId: Int
    var trackName: String
    var collectionName: String
}
struct Response: Codable {
    //our view model
    var results: [Result]
}

struct ContentView: View {
    @State private var results = [Result]()
    @State private var username = ""
    @State private var email = ""

    var body: some View {
        List(results, id: \.trackId) { item in
            VStack(alignment: .leading) {
                Text(item.trackName).font(.headline)
                Text(item.collectionName)
            }
        }
        .task {
            await loadData()
        }
//      DOES NOT SHOW UP IN CONSOLE, PART ONE:
        .onAppear(perform: {
            print("well, it appeared")
        })
//      Why not man?  (because the console was turned off!)
    }

    func loadData() async {

//      DOES NOT SHOW UP IN CONSOLE, PART TWO:
        print("inside loadData function")
//      Why not man?  (because the console was turned off!)

        guard let url = URL(string: "https:::://this url is soooo invalid") 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) {
                results = decodedResponse.results
            }
        } catch {
            print("invalid data")
        }
    }
}

   

Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try 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.