期间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
无法将“RealmSwift.DynamicObject”(0x1015c82d0)类型的值转换为“AppName.CustomObject”(0x1006e5550)。
我如何实现我想要的?目前我能想到的是创建一个 CustomObject 并手动为其分配值。
编辑 1
我想向CustomObject 添加一个primaryKey。我不断收到重复的主键错误,我很确定分配的键是唯一的。
致命错误:“试试!” 表达式意外引发错误:错误域 = io.realm 代码 = 0“主键属性 'resultKey' 在迁移后具有重复值。”
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
resultKey
我设法通过不设置主键来找出错误所在。该应用程序运行完美,当我打开.realm
查看值时,有些字段""
在resultKey
-> 重复的主键下。><