Protocol for class method

Daniel

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".

enter image description here

How can I work around this? I cannot use class func in the protocol... I'm not sure how to abstract this.

Thanks!

Martin R

In a class definition, static is an alias for class final, so it marks a type method (or property) which cannot be overridden in subclasses.

Since you want to override the method in subclasses, all you have to do is to define the method as class instead of static:

extension A: MyManagedObjectCoolStuff {

    class func entityName() -> String {
        return "Animal"
    }
}

extension B: MyManagedObjectCoolStuff {

    override class func entityName() -> String {
        return "Bat"
    }
}

extension C: MyManagedObjectCoolStuff {

    override class func entityName() -> String {
        return "Cat"
    }
}

Alternatively one could use the fact that for a Core Data entity, the class name is usually defined as <ModuleName>.<EntityName> so that the entity name is the last component of the class name.

So you could define entityName() as an extension method of NSManagedObject (the superclass of all Core Data object classes) as in How can I create instances of managed object subclasses in a NSManagedObject Swift extension?:

extension NSManagedObject {

    class func entityName() -> String {
        let classString = NSStringFromClass(self)
        // The entity is the last component of dot-separated class name:
        let components = split(classString) { $0 == "." }
        return components.last ?? classString
    }
}

and override it only where necessary:

class A: NSManagedObject { }

class B: A { }

class C: A { }

extension C {

    override class func entityName() -> String {
        return "Cat"
    }
}

println(A.entityName()) // A
println(B.entityName()) // B
println(C.entityName()) // Cat

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

'Self' is only available in a protocol or as the result of a class method

Swift protocol defining class method returning self

Custom method on class that conforms to MKAnnotation protocol

Can I override a class method in a protocol the class will conform to?

Method in non-final class must return `Self` to conform to protocol

Is it possible for a class to explicitly invoke a method on a protocol's extension?

"Type of a class which conforms to a protocol" as parameter in a method swift

@obj protocol delegate method not working in second class. Swift

Implement generic protocol method with but use generic for whole class

Protocol Method Is Not Calling

Clojure: No implementation of method in protocol

Is there a way to overload a method in a protocol?

Using method in protocol

Set breakpoint on protocol method

Method in protocol not implemented in iOS

Not able to pass self (which implements a protocol) to init method of a class instantiation.(Swift)

Call method on subclass to UITableViewCell with generic parameter value based on protocol or base class

Always the default implementation of protocol gets called even after implementing the method in class extension in an XCTest file

Inheritance in Class Vs Protocol

Class to protocol conversation in swift

Class does not conform to protocol

Swift Protocol of a particular class

Class using the iter protocol

weak Delegate and class protocol

Swift Protocol on a specific class?

Extend a Class that conforms a protocol?

Check if class conforms to protocol

Protocol inheritance from class?

Class Object to Protocol