我有一个Realm对象,其模型如下:
class WorkoutSet: Object {
// Schema 0
dynamic var exerciseName: String = ""
dynamic var reps: Int = 0
// Schema 0 + 1
dynamic var setCount: Int = 0
}
我正在尝试进行迁移。
在我的AppDelegate
中,我已经导入了RealmSwift
。
在didFinishLaunchWithOptions
函数中,我调用:
Migrations().checkSchema()
迁移是在另一个文件中声明的一个类。
在该文件中,有一个如下所示的结构体声明。
func checkSchema() {
Realm.Configuration(
// Set the new schema version. This must be greater than the previously used
// version (if you've never set a schema version before, the version is 0).
schemaVersion: 1,
// Set the block which will be called automatically when opening a Realm with
// a schema version lower than the one set above
migrationBlock: { migration, oldSchemaVersion in
// We haven’t migrated anything yet, so oldSchemaVersion == 0
switch oldSchemaVersion {
case 1:
break
default:
// Nothing to do!
// Realm will automatically detect new properties and removed properties
// And will update the schema on disk automatically
self.zeroToOne(migration)
}
})
}
func zeroToOne(migration: Migration) {
migration.enumerate(WorkoutSet.className()) {
oldObject, newObject in
let setCount = 1
newObject!["setCount"] = setCount
}
}
我在向模型添加setCount
时遇到了错误。