I want to declare in a protocol a class func
, I intend to conform to this protocol from a class A, B and C.
B and C inherit from A.
Essentially I want to override this func in B and C while still providing an implementation in A.
So, I had to declare my protocol as follows:
protocol MyManagedObjectCoolStuff {
static func entityName() -> String
}
And then I have this in A:
class A: NSManagedObject { }
class B: A { }
class C: A { }
extension A: MyManagedObjectCoolStuff {
static func entityName() -> String {
return "Animal"
}
}
extension B: MyManagedObjectCoolStuff {
override static func entityName() -> String {
return "Bat"
}
}
extension C: MyManagedObjectCoolStuff {
override static func entityName() -> String {
return "Cat"
}
}
The problem here, is clear and Xcode confirms: "Class method overrides a 'final' class method".
How can I work around this? I cannot use class func
in the protocol... I'm not sure how to abstract this.
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…