but found an array instead."-6ren"> but found an array instead."-现在我正面临这个错误 Value of type '[UserOrderHistory]' has no member 'orderlist' 我的 JSON: { "OrderList": [ -6ren">
gpt4 book ai didi

ios - 类型不匹配(Swift.Dictionary debugDescription : "Expected to decode Dictionary but found an array instead."

转载 作者:行者123 更新时间:2023-11-28 09:22:41 26 4
gpt4 key购买 nike

现在我正面临这个错误

Value of type '[UserOrderHistory]' has no member 'orderlist'

我的 JSON:

{
"OrderList": [
{
"orderId": 16976,
"userId": 4905,
"pickupdate": "2018-09-23",
},
{
"orderId": 52,
"userId": 4905,
"pickupdate": "2018-08-07",
},
],
"TotalOrder": 2
}

我的可解码模型类:

    class UserOrderHistory: Object, Decodable {

var orderlist: [OrderList]?
var TotalO = RealmOptional<Int>()

enum CodingKeys: String, CodingKey {
case orderlist = "OrderList"
case TotalO = "TotalOrder"
}
convenience required init(from decoder: Decoder) throws {
self.init()
let container = try decoder.container(keyedBy: CodingKeys.self)
self.orderlist = try container.decodeIfPresent(OrderList.self, forKey: .orderlist)
self.TotalO.value = try container.decodeIfPresent(Int.self, forKey: .TotalO)
}

required init() {
super.init()
}

required init(value: Any, schema: RLMSchema) {
super.init(value: value, schema: schema)
}

required init(realm: RLMRealm, schema: RLMObjectSchema) {
super.init(realm: realm, schema: schema)
}
}


class OrderList: Object, Decodable{
var orderId = RealmOptional<Int>()
var userId = RealmOptional<Int>()
@objc dynamic var pickupdate: String? = nil

enum CatCodingKeys: String, CodingKey {
case orderId
case userId
case pickupdate
}

convenience required init(from decoder: Decoder) throws {
self.init()
let container = try decoder.container(keyedBy: CatCodingKeys.self)
self.orderId.value = try container.decodeIfPresent(Int.self, forKey: .orderId)
self.userId.value = try container.decodeIfPresent(Int.self, forKey: .userId)

self.pickupdate = try container.decodeIfPresent(String.self, forKey: .pickupdate)

}

required init() {
super.init()
}

required init(value: Any, schema: RLMSchema) {
super.init(value: value, schema: schema)
}

required init(realm: RLMRealm, schema: RLMObjectSchema) {
super.init(realm: realm, schema: schema)
}
}

将服务器端数据解码为 Realm 兼容:

let decoder = JSONDecoder()
do {
let orders = try decoder.decode(Array<UserOrderHistory>.self, from: data)

try? realm!.write {
realm?.add(orders.orderlist!)
}

}catch {
print(error)
}

哪里出了问题。

  1. How I can solve this.
  2. Is there any other and easy way to parse this type of json to using decodable.

在顶部提到错误。

最佳答案

OrderList 是一个数组。您需要更改 UserOrderHistory 类中的变量 orderList:

var orderlist: [OrderList]?

仅保留此行,删除 Array:

let orders = try decoder.decode([UserOrderHistory].self, from: data)

到:

let orders = try decoder.decode(UserOrderHistory.self, from: data)

关于ios - 类型不匹配(Swift.Dictionary<Swift.String, Any> debugDescription : "Expected to decode Dictionary<String, Any> but found an array instead.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53777511/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com