问题
I'm new to Realm. I'm creating my models with a base class that inherits from Object, and custom subclasses of this base class. My model requires that the base class declares some properties as ignored by overriding the static ignoredProperties() method. When trying to override that method on some of the base class subclasses, I get a Swift compiler error stating that Class method overrides a 'final' class method. I don't have my base class implementation marked as final. I don't know if this is a current limitation with Realm but I can't seem to find any references to this issue.
My code looks like this:
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
}
}
Any ideas or suggestions? I'm currently using the latest Realm from CocoaPods, on current Xcode (7.2.1) and latest Swift.
回答1:
You've declared ignoredProperties as:
override static func ignoredProperties() -> [String]
It should be:
override class func ignoredProperties() -> [String]
static functions cannot be overridden by subclasses. class functions can.
回答2:
Accepted answer didn't work for me, but what did was removing the override from the parent class and adding the fields you want to ignore from the parent class in the child's implementation of ignoredProperties().
来源:https://stackoverflow.com/questions/35618754/implementing-ignoredproperties-on-both-a-object-subclass-and-its-own-subclass