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

SOLVED: Project 7: Day 1

Forums > 100 Days of Swift

I am stuck with nothing showing in the table view at all no title no subtitles just blank empty white screen

class ViewController: UITableViewController {
    var petitions = [Petition]()

    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(data)
            }
        }
    }

    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 {
        print(petitions)
        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]
        print(petitions)
        cell.textLabel?.text = petition.title
        cell.detailTextLabel?.text = petition.body
        return cell
    }

}

3      

Instead of

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

You should use URLSession, Data(contentsOf:) works with Bundle files.


if let url = URL(string: url) {
    do {
        let (data, _) = try await URLSession.shared.data(from: url)
        parse(data)
    } catch {
        print(error)
    }
}

Also you should mark the function async as it would download data from internet

3      

In project 7 he still didn't explain async and await he said he will in project 9 and it worked fine with him, I understand that I need async but at the same time I didn't learn that yet also I tried it and it gives me an error swift:13:19 Method does not override any method from its superclass"

I tried to do my own debugging and I put this

func parse(_ json: Data){
        let decoder = JSONDecoder()
        print("halo")

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

    }

turns out the else in if let is the one running and I have no clue why

Edit: Welp retyping a line of code for some reason worked even tho it is the same

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.