简体   繁体   中英

found nil while populating modal from JSON parsing in Swift4

I'd like to parse the following retrieved data using Swift 4.

{"bannerCategory":{"name":"","apps":[{"ImageName":"enhancedWith3dTouch"},{"ImageName":"drSeussBanner"},{"ImageName":"indieGameBanner"},{"ImageName":"clashRoyaleBanner"}],"type":""},"categories":[{"name":"Best New Apps","apps":[{"Id":1,"Name":"Disney Build It: Frozen","Category":"Entertainment","Price":3.99,"ImageName":"frozen"},{"Name":"Spot - the best places according to experts and friends","Category":"Travel","ImageName":"spot"},{"Name":"Dine - More Dates, Not Swipes","Category":"Social Networking","ImageName":"dine"},{"Name":"Today: Habit tracker for your daily goals and routines","Category":"Health \& Fitness","ImageName":"today"},{"Id":1,"Name":"Disney Build It: Frozen","Category":"Entertainment","Price":3.99,"ImageName":"frozen"},{"Name":"Spot - the best places according to experts and friends","Category":"Travel","ImageName":"spot"},{"Name":"Dine - More Dates, Not Swipes","Category":"Social Networking","ImageName":"dine"},{"Name":"Today: Habit tracker for your daily goals a nd routines","Category":"Health \& Fitness","ImageName":"today"}],"type":""},{"name":"Best New Games","apps":[{"Id":2,"Name":"Telepaint","Category":"Games","Price":2.99,"ImageName":"telepaint"},{"Name":"Dirac","Category":"Games","Price":1.99,"ImageName":"dirac"},{"Name":"Clash Royale","Category":"Games","ImageName":"clashroyale"},{"Name":"Beat Stomper","Category":"Games","Price":1.99,"ImageName":"beatstomper"},{"Id":2,"Name":"Telepaint","Category":"Games","Price":2.99,"ImageName":"telepaint"},{"Name":"Dirac","Category":"Games","Price":1.99,"ImageName":"dirac"},{"Name":"Clash Royale","Category":"Games","ImageName":"clashroyale"},{"Name":"Beat Stomper","Category":"Games","Price":1.99,"ImageName":"beatstomper"}],"type":""},{"name":"50% Off for a Limited Time","apps":[{"ImageName":"luminocity"},{"ImageName":"theroom"},{"ImageName":"badland"},{"ImageName":"shadowmatic"},{"ImageName":"luminocity"},{"ImageName":"theroom"},{"ImageName":"badland"},{"ImageName":"shadowmatic"}],"type":"large" },{"name":"More Games You Might Like","apps":[{"Name":"Summoners War","Category":"Games","ImageName":"summonerswar"},{"Name":"Angry Birds Space","Category":"Games","Price":0.99,"ImageName":"angrybirdsspace"},{"Name":"Star Wars™: Commander - Worlds in Conflict","Category":"Games","ImageName":"starwars"},{"Name":"Death Worm","Category":"Games","Price":2.99,"ImageName":"deathworm"}],"type":""}]}

modal object to retrieve data

struct FeaturedApps: Codable {

    var bannerCategory: AppCategory?
    var categories: [AppCategory]?



    struct AppCategory: Codable {

       let name : String?
       let apps : [App]?
       let type: String?

        struct App : Codable {

            let id: Int?
            let name: String?
            let category : String?
            let imageName : String?
            let price : Float?

        }   
     } 
}

codes for JSON parsing

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()


        let jsonUrl = "https://api.letsbuildthatapp.com/appstore/featured"
                guard let url = URL(string: jsonUrl) else { return }


       let task = URLSession.shared.dataTask(with: url) { (data, response, error)  in


            guard let data = data else { return }

            if let error = error {
                print(error)
                return
            }
            do {


                let courses = try JSONDecoder().decode(FeaturedApps.self, from: data)
                print(courses)



            } catch let jsonErr {
                print("Error serializing json:", jsonErr)
            }
        }
        task.resume()
     } 
}

That yields the following output:

FeaturedApps(bannerCategory: Optional(JsonParseSwift4.FeaturedApps.AppCategory(name: Optional(""), apps: Optional([JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil)]), type: Optional(""))), categories: Optional([JsonParseSwift4.FeaturedApps.AppCategory(name: Optional("Best New Apps"), apps: Optional([JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil)]), type: Optional("")), JsonParseSwift4.FeaturedApps.AppCategory(name: Optional("Best New Games"), apps: Optional([JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil)]), type: Optional("")), JsonParseSwift4.FeaturedApps.AppCategory(name: Optional("50% Off for a Limited Time"), apps: Optional([JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), Js onParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil)]), type: Optional("large")), JsonParseSwift4.FeaturedApps.AppCategory(name: Optional("More Games You Might Like"), apps: Optional([JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil), JsonParseSwift4.FeaturedApps.AppCategory.App(id: nil, name: nil, category: nil, imageName: nil, price: nil)]), type: Optional(""))]))

Any pointers as to what I'm doing wrong and getting nil values ?

The name of your properties should be the same as it is in the JSON response. If a property uses different naming convention(case and/or spelling), it is treated as not found and is assigned a value of nil (if it is optional) or the program crashes (if it is required)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM