当我想为一个类提供初始化程序时,我无法让Realm工作,Xcode无休止地建议错误.
我决定上传两个屏幕截图而不是代码段,以便更容易看到错误
我按照这些建议结束了这一点
最后一个错误告诉"使用未声明的类型'RLMObjectSchema'
我使用最新的0.99版本的RealmSwift
kis*_*umi 13
推荐的方法是创建成员便利初始化程序,如下所示:
class Item: Object {
dynamic var isBook: Bool = true
dynamic var numberOfPages: Double = 0
dynamic var isInForeignLanguage: Bool = true
dynamic var isFictional: Bool = true
dynamic var value: Int {
get {
return calculalatedValue()
}
}
convenience init(isBook: Bool, numberOfPages: Double, isInForeignLanguage: Bool, isFictional: Bool) {
self.init()
self.isBook = isBook
self.numberOfPages = numberOfPages
self.isInForeignLanguage = isInForeignLanguage
self.isFictional = isFictional
}
...
}
Run Code Online (Sandbox Code Playgroud)
您不能省略默认初始值设定项,因为Realm需要默认初始值设定项来实例化查询对象.在查询Realm时,Realm在内部调用默认初始化程序来实例化对象.
您也可以覆盖默认初始值设定项,但我们不建议使用它.因为当您覆盖默认初始化程序时,由于Swift类型系统限制,您应该覆盖从ObjC层继承的其他所需初始化程序.您还应该导入两者Realm
和RealmSwift
框架.因为在这些初始值设定项的参数中只有Objective-C类.
import RealmSwift
import Realm // Need to add import if you override default initializer!
class Item: Object {
dynamic var isBook: Bool = true
dynamic var numberOfPages: Double = 0
dynamic var isInForeignLanguage: Bool = true
dynamic var isFictional: Bool = true
dynamic var value: Int {
get {
return calculalatedValue()
}
}
required init() {
super.init()
}
required init(realm: RLMRealm, schema: RLMObjectSchema) {
super.init(realm: realm, schema: schema)
}
required init(value: AnyObject, schema: RLMSchema) {
super.init(value: value, schema: schema)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1642 次 |
最近记录: |