我刚刚为我的Realm模型添加了主键,因此我一直收到以下错误。我试图在appdelegate中迁移,但仍然得到了错误。我所做的只是添加propertyKey()
功能。我如何正确迁移?在Realm中迁移主键
Migration is required for object type 'Organization' due to the following errors:
- Property 'id' has been made a primary key."
但是我已经低于媒体链接添加的appdelegate:
Realm.Configuration.defaultConfiguration = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {
// The enumerate(_:_:) method iterates
// over every Person object stored in the Realm file
migration.enumerate(Organization.className()) { oldObject, newObject in
// combine name fields into a single field
}
}
})
这里是我的对象
class Location: Object {
var id: Int = 0
var longitude: Double = 0
var latitude: Double = 0
override class func primaryKey() -> String {
return "id"
}
}
class Organization: Object {
var id: Int = 0
var name: String = ""
var image: NSData = NSData()
let locations = List<Location>()
override class func primaryKey() -> String {
return "id"
}
}
所以它会是newObject![“primaryKeyProperty”] = 0(或任何其他int) –