我有一個問題以來,最近2天。我無法將我的JSON轉換爲Realm對象。Realm + Swift,嵌套JSON
我有一個JSON象下面這樣:
{
"gender" : "male",
"id" : "123456789",
"age_range" : {
"min" : 21
},
"last_name" : "LastName"
}
我有這樣的境界型號:
class UserObject: Object {
dynamic var userId: String = ""
dynamic var lastName: String?
dynamic var gender: String?
var ageRange = List<AgeRangeObject>()
required convenience init?(_ map: Map) {
self.init()
}
}
class AgeRangeObject: Object {
dynamic var min: Int = 0
}
,我試圖創建這個模型ObjectMapper解析JSON,以實例的方式字典,然後創建模型實例:
let userJSONModel = Mapper<User>().map(jsonString)
let realm = try! Realm()
do {
try realm.write {
let dict: [String : AnyObject] = [
"userId" : (userJSONModel?.userId)!,
"ageRange" : (userJSONModel?.ageRange)!,
"lastName" : (userJSONModel?.lastName)!,
"gender" : (userJSONModel?.gender)!
]
let userModel = UserObject(value: dict)
realm.add(userModel)
}
} catch {
print("Exception")
}
問題發生在此行上:let userModel = UserObject(value: dict)
我得到如下因素的錯誤:
*** Terminating app due to uncaught exception 'RLMException', reason: 'Invalid value 'min' to initialize object of type 'AgeRangeObject': missing key 'min''
我一直在尋找的計算器:
Nested Arrays throwing error in realm.create(value: JSON) for Swift
How to convert Realm object to JSON with nested NSDate properties?
,但我的情況是不同的。 你知道那個年齡段詞典有什麼問題嗎?爲什麼它不能很好地解析它? 謝謝。
非常感謝你! :)現在我開悟了;)我認爲Realm文檔並不清楚這種情況。 – PiotrCh