迁移期间无法将类型 'RealmSwift.DynamicObject' 的值转换为 MyCustomObject
Could not cast value of type 'RealmSwift.DynamicObject' to MyCustomObject during migration
在 RealmSwift migration
期间,我想从 dynamic var customObject: CustomObject
迁移到 let customObjectList = List<CustomObject>()
。 CustomObject
是 Object
的类型
这是迁移中的代码块
let newList = List<CustomObject>()
if oldObject!["customObject"] != nil {
print(oldObject!["customObject"])
var obj = oldObject!["customObject"]
var result: CustomObject = obj as! CustomObject //Crash
farList.append(result)
}
newObject!["customObjectList"] = newList
Could not cast value of type 'RealmSwift.DynamicObject' (0x1015c82d0) to 'AppName.CustomObject' (0x1006e5550).
我如何实现我想要的?目前我能想到的是创建一个 CustomObject 并手动为其分配值。
编辑 1
我想给 CustomObject 添加一个主键。我不断收到重复的主键错误,我很确定分配的键是唯一的。
fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=0 "Primary key property 'resultKey' has duplicate values after migration."
migration.deleteData(CustomObject.className())
if oldObject!["customObject"] != nil {
let oldSubFar = oldObject!["customObject"] as! MigrationObject
var newFarDict = oldSubFar.dictionaryWithValuesForKeys(["firstName","secondName"])
newFarDict["resultKey"] = NSUUID().UUIDString + "v1"
let newSubFar = migration.create(CustomObject.className(), value: newFarDict )
print(newSubFar) //its the updated object that i want
let subFarList = newObject!["customObjectList"] as! List<MigrationObject>
subFarList.append(newSubFar)
}
编辑 2
I manage to figure out what is the error by making resultKey
not a primary key. The app runs perfectly and when i open .realm
to see the values, there are some fields with ""
under resultKey
-> The duplicated primary key. ><
我想你想做的是:
如果需要,删除所有 CustomObject
数据,因为迁移列表对象无法附加现有对象。
然后您可以枚举 User
个对象,并从 User
的 属性 创建每个 CustomObject
。并且新的 User
对象有 customObject
属性,然后将 CustomObject
对象添加到列表中。
migration.deleteData(CustomObject.className()) // If needed
migration.enumerate(User.className()) { oldObject, newObject in
if let oldObject = oldObject,
let newObject = newObject {
let oldCustomObject = oldObject["customObject"] as! MigrationObject
let newCustomObject = migration.create(CustomObject.className(), value: oldCustomObject)
let customObjectList = newObject["customObjectList"] as! List<MigrationObject>
customObjectList.append(newCustomObject)
}
}
在 RealmSwift migration
期间,我想从 dynamic var customObject: CustomObject
迁移到 let customObjectList = List<CustomObject>()
。 CustomObject
是 Object
这是迁移中的代码块
let newList = List<CustomObject>()
if oldObject!["customObject"] != nil {
print(oldObject!["customObject"])
var obj = oldObject!["customObject"]
var result: CustomObject = obj as! CustomObject //Crash
farList.append(result)
}
newObject!["customObjectList"] = newList
Could not cast value of type 'RealmSwift.DynamicObject' (0x1015c82d0) to 'AppName.CustomObject' (0x1006e5550).
我如何实现我想要的?目前我能想到的是创建一个 CustomObject 并手动为其分配值。
编辑 1
我想给 CustomObject 添加一个主键。我不断收到重复的主键错误,我很确定分配的键是唯一的。
fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=0 "Primary key property 'resultKey' has duplicate values after migration."
migration.deleteData(CustomObject.className())
if oldObject!["customObject"] != nil {
let oldSubFar = oldObject!["customObject"] as! MigrationObject
var newFarDict = oldSubFar.dictionaryWithValuesForKeys(["firstName","secondName"])
newFarDict["resultKey"] = NSUUID().UUIDString + "v1"
let newSubFar = migration.create(CustomObject.className(), value: newFarDict )
print(newSubFar) //its the updated object that i want
let subFarList = newObject!["customObjectList"] as! List<MigrationObject>
subFarList.append(newSubFar)
}
编辑 2
I manage to figure out what is the error by making
resultKey
not a primary key. The app runs perfectly and when i open.realm
to see the values, there are some fields with""
underresultKey
-> The duplicated primary key. ><
我想你想做的是:
如果需要,删除所有 CustomObject
数据,因为迁移列表对象无法附加现有对象。
然后您可以枚举 User
个对象,并从 User
的 属性 创建每个 CustomObject
。并且新的 User
对象有 customObject
属性,然后将 CustomObject
对象添加到列表中。
migration.deleteData(CustomObject.className()) // If needed
migration.enumerate(User.className()) { oldObject, newObject in
if let oldObject = oldObject,
let newObject = newObject {
let oldCustomObject = oldObject["customObject"] as! MigrationObject
let newCustomObject = migration.create(CustomObject.className(), value: oldCustomObject)
let customObjectList = newObject["customObjectList"] as! List<MigrationObject>
customObjectList.append(newCustomObject)
}
}