我是 Realm 的新手。我正在使用继承自 Object 的基类和该基类的自定义子类来创建我的模型。我的模型要求基类通过覆盖静态 ignoredProperties()
方法将某些属性声明为忽略。当尝试在某些基类子类上覆盖该方法时,我收到一个 Swift 编译器错误,指出 类方法覆盖了“最终”类方法。我没有将基类实现标记为 final
。我不知道这是否是 Realm 的当前限制,但我似乎找不到关于此问题的任何引用资料。
我的代码是这样的:
class Base: Object {
// properties declarations
override static func ignoredProperties() -> [String] {
return ["someProperty"]
}
}
class SomeModel: Base {
// properties declarations
// compiler error here
override static func ignoredProperties() -> [String] {
var ignoredProperties = super.ignoredProperties()
ignoredProperties.append("someOtherProperty")
return ignoredProperties
}
}
有什么想法或建议吗?我目前在当前的 Xcode (7.2.1) 和最新的 Swift 上使用来自 CocoaPods 的最新 Realm。
最佳答案
您已将 ignoredProperties
声明为:
override static func ignoredProperties() -> [String]
应该是:
override class func ignoredProperties() -> [String]
static
函数不能被子类覆盖。 class
函数可以。
关于swift - 在 Object 子类和它自己的子类上实现 ignoredProperties(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35618754/