RLMException:迁移到Swift 4的对象'Book'上不存在'主键属性'serial' [英] RLMException: 'Primary key property 'serial' does not exist on object 'Book' Migrating to Swift 4
问题描述
我正在使用Swift 4编译在iOS上使用Realm遇到此问题,在启动时应用程序崩溃并显示以下消息
I'm running into this issue with Realm on iOS using Swift 4 compilation, where on startup the app crashes with the following message
RLMException', reason: 'Primary key property 'serial' does not exist on object 'Book''
我看到了类似的错误消息,但不是相同的错误消息. 这就是我的对象的样子
I saw similar error messages, but not the same one. This is what my object looks like
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"
}
}
当我通过Realm Browser应用程序检查default.realm文件时,我注意到条目中只有#(0,1,2),并且其中没有数据.如果我注释掉主键,它将运行,但是在Realm中没有存储该对象.无法弄清楚为什么它崩溃了!
When I checked the default.realm file through the Realm Browser app, I noticed that the entries only have a # (0,1,2) and no data in it. If I comment out the primary key, it runs, but nothing is stored in Realm for this object. Can't figure out why it's crashing!
推荐答案
在Realm中,模型的属性必须具有@objc dynamic var
属性,这就是我所缺少的.
In Realm, the properties of your model have to have the @objc dynamic var
attribute, that is what I was missing.
来自领域网站:
领域模型属性必须具有
@objc dynamic var
属性,才能成为基础数据库数据的访问器.请注意,如果将该类声明为@objcMembers
(Swift 4或更高版本),则可以将各个属性声明为dynamic var
.
Realm model properties must have the
@objc dynamic var
attribute to become accessors for the underlying database data. Note that if the class is declared as@objcMembers
(Swift 4 or later), the individual properties can just be declared asdynamic var
.
这篇关于RLMException:迁移到Swift 4的对象'Book'上不存在'主键属性'serial'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!