Swift Realm Property’*’已被添加到最新的对象模型MIGRATION中

前端之家收集整理的这篇文章主要介绍了Swift Realm Property’*’已被添加到最新的对象模型MIGRATION中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经将新的数组属性添加到R​​LMObject和
public class Student: RLMObject {
    dynamic var id = 0
    dynamic var name = ""
    dynamic var resultList = RLMArray(objectClassName:Result.className())
}

public class Result: RLMObject {
}

错误日志:

Migration is required for object type ‘Student’ due to the following
errors:
– Property ‘resultList’ has been added to latest object model.

TRY失败:

let configuration:RLMRealmConfiguration = RLMRealmConfiguration.defaultConfiguration()

migration.enumerateObjects(Student.className()) { oldObject,newObject in
    newObject!["resultList"] = RLMArray(objectClassName: Result.className())
}

编辑:

let configuration:RLMRealmConfiguration = RLMRealmConfiguration.defaultConfiguration()
    print("Realm db current version: \(configuration.schemaVersion)")
    configuration.schemaVersion = 1
    configuration.migrationBlock = {(migration:RLMMigration,oldSchemaVersion: UInt64) in
        print("Realm db migration start")
        if oldSchemaVersion < 1 {
            print("Schema version: 1 - Rename fields")
            migration.enumerateObjects(Student.className()) { oldObject,newObject in
                newObject!["creationDate"] = oldObject!["createdAt"]
                newObject!["modifiedDate"] = oldObject!["updatedAt"]
            }
        }
        print("Realm db migration finish")
    }
    RLMRealmConfiguration.setDefaultConfiguration(configuration)
    let realm = RLMRealm.defaultRealm()

解:

将您的版本更新为1

configuration.schemaVersion += 1
您必须增加schemaVersion并在RLMRealmConfiguration上提供migrationBlock.在那里你可以迁移表.但是你不需要在你的具体情况下.可以自动处理属性添加.你还需要一个空块.
let config = RLMRealmConfiguration.defaultConfiguration()
config.schemaVersion = 1
config.migrationBlock = { (migration,oldSchemaVersion) in
    // nothing to do
}
RLMRealmConfiguration.setDefaultConfiguration(config)
原文链接:https://www.f2er.com/swift/319930.html

猜你在找的Swift相关文章