RLMException:'对象'Book'迁移到Swift 4上不存在'主键属性'serial'

Hel*_*ffy 11 realm ios swift

我在iOS上使用Swift 4编译遇到了这个问题,在启动时应用程序崩溃时出现以下消息

RLMException', reason: 'Primary key property 'serial' does not exist on object 'Book''
Run Code Online (Sandbox Code Playgroud)

我看到了类似的错误消息,但不是同一个.这就是我的对象的样子

import Foundation
import RealmSwift

class Book: Object {
    dynamic var serial: String = ""
    dynamic var title: String = ""
    dynamic var pages: Int = 0
    dynamic var genre: String = ""

    override static func primaryKey() -> String? {
        return "serial"
    }
}
Run Code Online (Sandbox Code Playgroud)

当我通过Realm Browser应用程序检查default.realm文件时,我注意到这些条目只有#(0,1,2)并且没有数据.如果我注释掉主键,它会运行,但Realm中没有任何内容存储在此对象中.无法弄清楚为什么它会崩溃!

Gal*_*har 41

Although it doesn't necessarily about migration, there's an issue with iOS 13 and Xcode 11 which may cause this problem. All String properties of Realm classes with a default String value set are disregarded somehow. You can fix this by updating to the latest version (currently 3.20.0) and than on Xcode: Product -> Clean Build Folder.

If you're using cocoa-pods, do this:

Open your project's Podfile, and replace RealmSwift line with:

pod 'RealmSwift', '~> 3.20.0'
Run Code Online (Sandbox Code Playgroud)

Then, open terminal on the project's folder and:

pod repo update
pod install
Run Code Online (Sandbox Code Playgroud)

Hope that helps.


Hel*_*ffy 21

在Realm中,模型的属性必须具有@objc动态var属性,这就是我所缺少的.

来自Realm网站:

__PRE__


Kha*_*lam 6

import Foundation
import RealmSwift

class Book: Object {
   @objc dynamic var id : Int = 0
   @objc dynamic var serial: String = ""
   @objc dynamic var title: String = ""
   @objc dynamic var pages: Int = 0
   @objc dynamic var genre: String = ""

    override static func primaryKey() -> String? {
        return "id"
    }
}
Run Code Online (Sandbox Code Playgroud)