我必须使用EasyMapping。当我从Json解析字典时,它工作得很好。但现在我只需要解析一系列对象,所以我没有任何关键路径......我有Json:
[
{
"key1": "dee",
"key2": 3232
},
{
"key1": "deeweewregre",
"key2": 5555
}
]
所以我创建了2个自定义类。第一个是数组中的元素:
class TheEntry: EKObjectModel {
var key1: String!
var key2: Int!
}
extension TheEntry {
override class func objectMapping() -> EKObjectMapping{
let mapping = EKObjectMapping(objectClass: self)
mapping.mapPropertiesFromArray(["key1", "key2"])
return mapping
}
}
另一个用于阵列本身:
class TheList: EKObjectModel {
var entries: [TheEntry]!
}
extension TheList {
override class func objectMapping() -> EKObjectMapping{
let mapping = EKObjectMapping(objectClass: self)
mapping.hasMany(TheEntry.self, forKeyPath: mapping.rootPath)
return mapping
}
}
但它不起作用。我的应用程序在mapping.rootPath上崩溃。但我不知道如何使用Json的根级别。请问有人伸出援助之手吗?
答案 0 :(得分:0)
试试这个
mapping.hasMany(TheEntry.self, forKeyPath: "@self")