WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Project 7, Day 1 - No parsed data shown

Forums > 100 Days of Swift

@daryb  

Hello,

I've gone through the first day of Project 7 and when I build the app, it doesn't show any parsed data (title or body) in the Table View Controller. In the debug window I get the following message:

nw_protocol_get_quic_image_block_invoke dlopen libquic failed

I've also downloaded the project from the HWS Github page to make sure it wasn't something I messed up while following the tutorial, and I get the same result when trying to build; no data showing up, and same error message.

Any suggestions for next steps? Thanks!

Xcode 12.4

1      

Hi is that the White House Petitions app?

1      

@daryb  

yes, it is the white house petition one - thanks!

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

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

Hello,

are you using the HWS url? The whitehouse.gov url is not working.

1      

@daryb  

Thanks plrichardson - yes, same outcome whether I use the cached HWS url or the real whitehouse feed.

1      

Pretty sure that error is something you can ignore. It started popping up with Xcode 12 and doesn't prevent apps from running.

Not sure why you can't get even Paul's project to work. My version of Project 7 works once I swap out the hackingwithswift.com URL for the non-functioning whitehouse.gov one. And if I download Paul's github repository and run that project, it also works after swapping out the URL.

Maybe post your ViewController.swift code so we can take a look? Although, that won't explain why you can't get Paul's project to work but it's a start.

1      

@daryb  

Thanks @roosterboy - sorry for the delay. Because Paul's project has the same result, I'm also stumped on this one. Here's my ViewController.swift code though in case that does help:

import UIKit

class ViewController: UITableViewController {

    var petitions = [Petition]()

    func parse(json: Data) {
        let decoder = JSONDecoder()

        if let jsonPetitions = try? decoder.decode(Petitions.self, from: json) {
            petitions = jsonPetitions.results
            tableView.reloadData()
        }
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return petitions.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        let petition = petitions[indexPath.row]
        cell.textLabel?.text = petition.title
        cell.detailTextLabel?.text = petition.body
        return cell
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // let urlString = "https://api.whitehouse.gov/v1/petitions.json?limit=100"
        let urlString = "https://www.hackingwithswift.com/samples/petitions-1.json"

        if let url = URL(string: urlString) {
            if let data = try? Data(contentsOf: url) {
                parse(json: data)
            }
        }
    }
}

1      

Interesting...

If I replace the ViewController in my own project with yours, it works.

All I can think is that maybe the URL is being blocked on your system somehow? What happens if try to go to the JSON's URL in a browser window?

1      

@daryb  

Thanks for checking the file - when I go to the json feed in a browser it shows up correctly. I also disabled my VPN (which has some blocking as well), just to make sure, and I'm still getting the same result. hmmm....

1      

i am having the same problem....

1      

@n35k4  

Hello guys,

I had exactly the same issue, and managed to solve it.

First, looks like the whitehouse API is not working anymore,so the following domains(or any related to the API itself) will not work : https://api.whitehouse.gov/v1/petitions.json?limit=100 and https://api.whitehouse.gov/v1/petitions.json?signatureCountFloor=10000&limit=100 So we will need to use Pauls: https://www.hackingwithswift.com/samples/petitions-1.json and https://www.hackingwithswift.com/samples/petitions-2.json

Second, the issue with no parsed data even with Pauls link was a silly typo from my side in the Petition object. I wrote var signatureCounter: Int instead of var signatureCount: Int .

This why for example when @roosterboy replaced the ViewController it started working on his end.

After fixing the typo, it all worked perfectly! So check carefully for any typos!!! Good luck guys and girls !

// This is the proper one
struct Petition: Codable {
    var title: String
    var body: String
    var signatureCount: Int
}

1      

Yet another example of why one should not decode JSON like this:

        if let jsonPetitions = try? decoder.decode(Petitions.self, from: json) {
            petitions = jsonPetitions.results
            tableView.reloadData()
        }

but should instead use a do {} catch {} block to handle any errors. That try? just swallows any errors and leaves one wondering what the heck is wrong. But if a do {} catch {} had been used instead:

        do {
            let jsonPetitions = try decoder.decode(Petitions.self, from: json)
            petitions = jsonPetitions.results
            tableView.reloadData()
        } catch {
            print(error) //handle it better in a real app
        }

there would have been a keyNotFound error for the signatureCounter property and the issue would have been much clearer.

2      

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

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.