让Nil尝试解码json

时间:2019-02-09 16:37:15

标签: swift

我正在尝试从此URL(:

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

来自“让我们构建该应用程序教程” 这是我写的代码:

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?
}

我尝试按照本教程进行操作,但对我而言不起作用。 在尝试从url解码json时,我总是得到nil。我真的很陌生,无法弄清楚我在做什么错。我知道要正确解码json,我需要具有与json相同的Structs(例如类别中的应用程序数组),但仍然无法正常工作。

编辑:应该提到的是,当我运行它时,代码进入catch块并显示“ nil”。

我尝试打印'err',但这是我在日志中得到的所有内容:

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

1 个答案:

答案 0 :(得分:1)

你有

  

typeMismatch(Swift.String,Swift.DecodingError.Context(codingPath:[CodingKeys(stringValue:“ bannerCategory”,intValue:nil)]],debugDescription:“希望对String进行解码,但是找到了字典。”,底层错误:nil ))

您需要

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