【问题标题】:I don't understand why my code works for an array of my struct but not with a struct containing an array of this struct?我不明白为什么我的代码适用于我的结构数组,但不适用于包含该结构数组的结构?
【发布时间】:2019-08-14 00:16:59
【问题描述】:

所以基本上我正在尝试读取有关一些支出的本地 json 文件。我有一个结构“支出”和一个包含支出数组的结构“支出”。当我使用 Spendings 类型解码时,我无法访问我的 json 中的数据。

我尝试使用有效的 [Spending.self] 进行解码,但我想使用我的 struct Spendings,但我不知道为什么它不起作用?

[
    {
        "id": 1,
        "name": "Métro 052",
        "price": 8.97,
        "date": "22/07/2019",
        "category": "Transport"
    },
    {
        "id": 2,
        "name": "National Geographic Museum",
        "price": 10.77,
        "date": "22/07/2019",
        "category": "Museum"
    }
]
enum Categories: String, Codable {
    case Transport
    case Food
    case Museum
    case Mobile
    case Housing
    case Gifts
    case Shopping
}

struct Spending: Codable {
    var id: Int
    var name: String
    var price: Float
    var date: String
    var category: Categories
}

struct Spendings: Codable {
    let list: [Spending]
}
//Not working
class SpendingController {
    static let shared = SpendingController()

    func fetchSpendings(completion: @escaping ([Spending]?) -> Void) {
        if let filepath = Bundle.main.path(forResource: "spending", ofType: "json") {
            let jsonDecoder = JSONDecoder()
            if let data = try? Data(contentsOf: URL(fileURLWithPath: filepath)), let spendings = try? jsonDecoder.decode(Spendings.self, from: data) {
                completion(spendings.list)
            }
        }
    }
}

//Working
class SpendingController {
    static let shared = SpendingController()

    func fetchSpendings(completion: @escaping ([Spending]?) -> Void) {
        if let filepath = Bundle.main.path(forResource: "spending", ofType: "json") {
            let jsonDecoder = JSONDecoder()
            if let data = try? Data(contentsOf: URL(fileURLWithPath: filepath)), let spendings = try? jsonDecoder.decode([Spending].self, from: data) {
                completion(spendings)
            }
        }
    }
}

我没有任何错误消息,但是在我完成打印结果时,与使用 [Spending].self 时相反,没有打印任何内容。

【问题讨论】:

    标签: arrays json swift struct


    【解决方案1】:

    在这里解码[Spending].self确实是正确的,因为你的JSON的根是一个数组,这意味着你用来解码的类型应该是[XXX].self

    在这里解码Spendings.self 是不正确的,因为这意味着您正在解码对象根,而不是数组根。 Spendings 结构只有一个属性 list,因此 JSON 的根对象需要有一个 "list" 的键才能正确解码 Spendings.self,如下所示:

    {
        "list":
            [
                {
                    "id": 1,
                    "name": "Métro 052",
                    "price": 8.97,
                    "date": "22/07/2019",
                    "category": "Transport"
                },
                {
                    "id": 2,
                    "name": "National Geographic Museum",
                    "price": 10.77,
                    "date": "22/07/2019",
                    "category": "Museum"
                }
            ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-16
      • 1970-01-01
      • 1970-01-01
      • 2021-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      相关资源
      最近更新 更多