【发布时间】:2018-07-30 23:15:43
【问题描述】:
我从领域文档中复制并粘贴代码。但是不知道如何更改我在旁边评论以指示的部分。 (在底部) 以下是我收到的完整错误消息:
error initializing newrealm, Error Domain=io.realm Code=10 "由于以下错误需要迁移: - 属性 'Item.dateCreated' 已添加。" UserInfo={NSLocalizedDescription=由于以下错误需要迁移: - 已添加属性“Item.dateCreated”。错误代码=10} 2018-07-30 21:25:24.231575-0400 Todoey[87561:3063712] *** 由于未捕获的异常“RLMException”而终止应用程序,原因:“类别”的属性名称“dateCreated”无效。”
以下是我尝试迁移的女巫中的代码:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
print(Realm.Configuration.defaultConfiguration.fileURL)
do {
let realm = try Realm()
} catch {
print("error initializing newrealm, \(error)")
}
//Migration
let config = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {
migration.enumerateObjects(ofType: Category.className()) { (old, new) in
new!["dateCreated"] = Date()
}
migration.enumerateObjects(ofType: Item.className()) { (old, new) in
new!["dateCreated"] = Date()
}
}
})
Realm.Configuration.defaultConfiguration = config
//Migration X
return true
}
问题似乎出在“//将名称字段合并为单个字段”注释所在的位置之上。我需要将这些值更改为以下内容:
class Item: Object {
@objc dynamic var title: String = ""
@objc dynamic var done: Bool = false
@objc dynamic var dateCreated = NSDate() //this is the new data
var parentCategory = LinkingObjects(fromType: Category.self, property: "items")
}
【问题讨论】:
-
你可以在这个答案中找到我的解决方案:https://stackoverflow.com/a/53979285/6013170
标签: ios swift migration realm realm-migration