Realm 迁移中的转换错误 属性 Double to List<CustomObject>
Casting error in Realm migration to change property Double to List<CustomObject>
我需要的是创建一个迁移,根据以下代码将 discount:Double
更改为 discounts:List<Discount>()
。
这是现有对象:这是我的 Realm Object
现在的样子。
class Item:Object{
@objc dynamic var itemName:String = "Some Name"
@objc dynamic var discount:Double = 0 // need to change this property
}
以下是我尝试进行更改的方式。
新对象:
class Discount: Object {
@objc dynamic var name:String = "Discount Name"
@objc dynamic var discount:Double = 0.0
}
修改对象具有one-to-many
关系。
class Item:Object{
@objc dynamic var itemName:String = "Some Name"
let discounts = List<Discount>()
}
迁移
Realm.Configuration.defaultConfiguration = Realm.Configuration(
schemaVersion: 3,
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 3) {
migration.enumerateObjects(ofType: Item.className()) { oldObject, newObject in
let oldDiscount = oldObject!["discount"] as! Double
let discount = Discount()
discount.amount = oldDiscount
let discounts = newObject!["discounts"] as! List<Discount>
discounts.append(discount)
}
}
})
我遇到错误:
Could not cast value of type RealmSwift.List<RealmSwift.DynamicObject>(0x1c8910068)
to RealmSwift.List<AppName.Discount> (0x1c890f6a0)
.
同样,问题是,我如何创建迁移以将 discount:Double
更改为 discounts:List<Discount>()
?
您可以将折扣 属性 作为 [oldDiscount]
之类的列表迁移到新的折扣列表中
这是代码。我重命名了一些对象以使其更加清晰,因为重新使用单词 'discount' 来指代 class 和 属性 令人困惑。
let vers = UInt64(1)
let config = Realm.Configuration( schemaVersion: vers, migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < vers) {
print("performing migration")
migration.enumerateObjects(ofType: ItemClass.className()) { oldItem, newItem in
let oldDiscount = oldItem!["discount"] as? Double ?? 0.0
let d = DiscountClass()
d.name = "Test"
d.discount = oldDiscount
newItem!["discountList"] = [d]
}
} else {
print("no migration needed")
}
})
我需要的是创建一个迁移,根据以下代码将 discount:Double
更改为 discounts:List<Discount>()
。
这是现有对象:这是我的 Realm Object
现在的样子。
class Item:Object{
@objc dynamic var itemName:String = "Some Name"
@objc dynamic var discount:Double = 0 // need to change this property
}
以下是我尝试进行更改的方式。
新对象:
class Discount: Object {
@objc dynamic var name:String = "Discount Name"
@objc dynamic var discount:Double = 0.0
}
修改对象具有one-to-many
关系。
class Item:Object{
@objc dynamic var itemName:String = "Some Name"
let discounts = List<Discount>()
}
迁移
Realm.Configuration.defaultConfiguration = Realm.Configuration(
schemaVersion: 3,
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 3) {
migration.enumerateObjects(ofType: Item.className()) { oldObject, newObject in
let oldDiscount = oldObject!["discount"] as! Double
let discount = Discount()
discount.amount = oldDiscount
let discounts = newObject!["discounts"] as! List<Discount>
discounts.append(discount)
}
}
})
我遇到错误:
Could not cast value of type
RealmSwift.List<RealmSwift.DynamicObject>(0x1c8910068)
toRealmSwift.List<AppName.Discount> (0x1c890f6a0)
.
同样,问题是,我如何创建迁移以将 discount:Double
更改为 discounts:List<Discount>()
?
您可以将折扣 属性 作为 [oldDiscount]
之类的列表迁移到新的折扣列表中这是代码。我重命名了一些对象以使其更加清晰,因为重新使用单词 'discount' 来指代 class 和 属性 令人困惑。
let vers = UInt64(1)
let config = Realm.Configuration( schemaVersion: vers, migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < vers) {
print("performing migration")
migration.enumerateObjects(ofType: ItemClass.className()) { oldItem, newItem in
let oldDiscount = oldItem!["discount"] as? Double ?? 0.0
let d = DiscountClass()
d.name = "Test"
d.discount = oldDiscount
newItem!["discountList"] = [d]
}
} else {
print("no migration needed")
}
})