gpt4 book ai didi

ios - 将 Realm 2.x 对象列表迁移到 Realm 3.x 字符串列表(或其他主要类型)

转载 作者:行者123 更新时间:2023-11-28 12:15:18 25 4
gpt4 key购买 nike

Realm 3.0 引入了基本类型列表(String、Int、...)。

我想将仅包含原始类型的一个属性的自定义对象的旧列表迁移到那些更简单的列表,但我不明白在 Realm 迁移 block 期间如何进行。

我在 Realm 2.0(简化版)中使用的内容:

class Map: Object {
let cities = List<City>()
}

class City: Object {
@objc dynamic var name: String = ""

override static func indexedProperties() -> [String] {
return ["name"]
}
}

我想在 Realm 3.0 中使用什么:

class Map: Object {
let cities = List<String>()

override static func indexedProperties() -> [String] {
return ["cities"]
}
}

如何进行迁移?以下不起作用。

if oldSchemaVersion < 2 {
migration.enumerateObjects(ofType: Map.className(), { (oldObject, newObject) in
newObject!["cities"] = oldObject!["cities"] as! List<String>
})
}

不确定 indexedProperties() 以及这些新列表的使用情况。

最佳答案

您需要将城市列表转换为字符串列表:

if oldSchemaVersion < 2 {
migration.enumerateObjects(ofType: Map.className(), { (oldObject, newObject) in
newObject!["cities"] = (oldObject!["cities"] as! List<MigrationObject>).value(forKey: "name")
})
}

不支持索引 List 属性。

关于ios - 将 Realm 2.x 对象列表迁移到 Realm 3.x 字符串列表(或其他主要类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46837813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com