作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
but found an array instead."-6ren"> but found an array instead."-现在我正面临这个错误 Value of type '[UserOrderHistory]' has no member 'orderlist' 我的 JSON: { "OrderList": [ -6ren">
现在我正面临这个错误
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)
}
哪里出了问题。
- How I can solve this.
- 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/
我是一名优秀的程序员,十分优秀!