John Doah
John Doah

Reputation: 1999

Getting Nil trying to decode json

I'm trying to decode the Json from this URL(:

https://api.letsbuildthatapp.com/appstore/featured

from "Lets build that app tutorials" This is the code I wrote:

import UIKit

class AppCategory: NSObject{
    var name: String?
    var apps: [App]?

    static func fetchedFeaturedApps(){
        let jsonUrlString = "https://api.letsbuildthatapp.com/appstore/featured"
        guard let url = URL(string: jsonUrlString) else {return}
        URLSession.shared.dataTask(with: url) { (data, response, err) in
            guard let data = data else {return}
            do{
                let featured = try JSONDecoder().decode(Featured.self, from: data)
            }catch{
                print(err)
            }
            }.resume()
    }
}

struct Featured: Decodable {
    var bannerCategory: Banner?
    var featuredCategories: [mCategory]?
}
struct Banner: Decodable {
    var name: String?
    var apps: [String]?
    var type: String?
}

struct mCategory: Decodable {
    var name: String?
    var apps: [App]?
    var type: String?
}

struct App: Decodable {
    var id: Int?
    var name: String?
    var category: String?
    var price: Float?
    var imageName: String?
}

I tried to follow the tutorial but It didn't work for me. I always get nil while trying to decode the json from the url. I'm really new to this and can't figure out what I'm doing wrong. I know to decode the json properly I need to have Structs as same as the json (like an app array in category) but it still won't work.

EDIT: Should've mentioned, When I run it, the code goes into the catch block and printing "nil".

I tried printing the 'err' but this is all I get in the log:

2019-02-09 19:07:45.241000+0200 AppStore[2344:120273] [AXMediaCommon] Unable to look up screen scale
2019-02-09 19:07:45.241153+0200 AppStore[2344:120273] [AXMediaCommon] Unexpected physical screen orientation
2019-02-09 19:07:45.314112+0200 AppStore[2344:120273] [AXMediaCommon] Unable to look up screen scale
2019-02-09 19:07:45.319977+0200 AppStore[2344:120273] [AXMediaCommon] Unable to look up screen scale
2019-02-09 19:07:45.320189+0200 AppStore[2344:120273] [AXMediaCommon] Unexpected physical screen orientation
nil

Upvotes: 0

Views: 126

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You have

typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "bannerCategory", intValue: nil)], debugDescription: "Expected to decode String but found a dictionary instead.", underlyingError: nil))

you need

struct Featured: Codable {
    let bannerCategory: BannerCategory
    let categories: [Category]
}

struct BannerCategory: Codable {
    let name: String
    let apps: [BannerCategoryApp]
    let type: String
}

struct BannerCategoryApp: Codable {
    let imageName: String

    enum CodingKeys: String, CodingKey {
        case imageName = "ImageName"
    }
}

struct Category: Codable {
    let name: String
    let apps: [CategoryApp]
    let type: String
}

struct CategoryApp: Codable {
    let id: Int?
    let name, category: String?
    let price: Double?
    let imageName: String

    enum CodingKeys: String, CodingKey {
        case id = "Id"
        case name = "Name"
        case category = "Category"
        case price = "Price"
        case imageName = "ImageName"
    }
}

class AppCategory: NSObject{
    var name: String?
    var apps: [CategoryApp]?

    static func fetchedFeaturedApps(){
        let jsonUrlString = "https://api.letsbuildthatapp.com/appstore/featured"
        guard let url = URL(string: jsonUrlString) else {return}
        URLSession.shared.dataTask(with: url) { (data, response, err) in
            guard let data = data else {return}
            do{
                let featured = try JSONDecoder().decode(Featured.self, from: data)
                print(featured)
            }catch{
                print(error)
            }
            }.resume()
    }
}

Upvotes: 1

Related Questions