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

SOLVED: Day 59 - Milestone - Parsed data doesn't appear in the table view

Forums > 100 Days of Swift

Hi guys,

I have a problem with the milestone, as the data made by myself in a custom .json file is parsed successfully, but without being shown in the table view.

I put the file in the bundle, then I parsed it:

let filePath = Bundle.main.url(forResource: "Towns", withExtension: "json")

        if let url = filePath {
            if let data = try? Data(contentsOf: url) {
                parse(json: data)
                print("data parsed")
                return
            } else {
                print("error")
            }
        } else {
            print("error")
        }

So, when I load the table view, I get "data parsed" message, but that's all, no data in the table view at all. It's worth noting that in parse() function I also included reloading the table view.

Now I think it might be the way I wrote the .json file, as I have no experience in doing it whatsoever. I wrote it like this:

[
{
    "town": "x",
    "info": "y",
    "worth it": "z"
  },
{
    "town": "x",
    "info": "y",
    "worth it": "z"
  },
{
    "town": "x",
    "info": "y",
    "worth it": "z"
  },
{
    "town": "x",
    "info": "y",
    "worth it": "z"
  },
{
    "town": "x",
    "info": "y",
    "worth it": "z"
  },
{
    "town": "x",
    "info": "y",
    "worth it": "z"
  }
]

Could the problem be my .json formatting? If yes, why the hell Xcode tells me that the data has been parsed succesfully?

3      

Aaaand indeed it was my .json file. What I should have done is added the name of the property, which brings up all .json info into a single array:

{ "results":
[
{
    "town": "x",
    "info": "y",
    "worth it": "z"
  },
{
    "town": "x",
    "info": "y",
    "worth it": "z"
  },
{
    "town": "x",
    "info": "y",
    "worth it": "z"
  },
{
    "town": "x",
    "info": "y",
    "worth it": "z"
  },
{
    "town": "x",
    "info": "y",
    "worth it": "z"
  },
{
    "town": "x",
    "info": "y",
    "worth it": "z"
  }
]
}

I had "var results" property in my town custom struct that I didn't refer to. Now everything's fine.

3      

Thanks but for me is it stil not working.

let filePath = Bundle.main.url(forResource: "countrieslist", withExtension: "json")
        if let url = filePath {
            if let data = try? Data(contentsOf: url) {
                parse(json: data)
                print("data parsed")
                print(countries.count)
                return
            } else {
                print("error")
            }
        } else {
            showError()
        }

With this as .json

{ "results":
[
    {
        "common": "Aruba",
        "official": "Aruba",
        "independent": false,
        "status": "officially-assigned",
        "unMember": false,
        "currencies": "Aruban florin",
        "capital": "Oranjestad"
        "region": "Americas",
        "subregion": "Caribbean",
        "languages": "Dutch",
    },
    {
        "common": "Afghanistan",
        "official": "Islamic Republic of Afghanistan",
        "independent": true,
        "status": "officially-assigned",
        "unMember": true,
        "currencies": "Afghan afghani",
        "capital": "Kabul"
        "region": "Asia",
        "subregion": "Southern Asia",
        "languages": "Dari",
    },
    {
        "common": "Angola",
        "official": "Republic of Angola",
        "independent": true,
        "status": "officially-assigned",
        "unMember": true,
        "currencies": "Angolan kwanza",
        "capital": "Luanda"
        "region": "Africa",
        "subregion": "Middle Africa",
        "languages": "Portuguese"
    },
]
}

this as results

struct Countries: Codable {
    var results: [Countrie]
}

And this as results:

struct Countrie: Codable {
    var common: String
    var official: String
    var independent: Bool
    var status: String
    var unMember: Bool
    var currencies: String
    var capital: String
    var region: String
    var subregion: String
    var languages: String
}

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.