BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

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      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.