Swift Declare Class Func in Protocol

You can review Apple’s Documentation (subsection Method Requirements).

There says:

As with type property requirements, you always prefix type method requirements with the static keyword when they are defined in a protocol. This is true even though type method requirements are prefixed with the class or static keyword when implemented by a class

In practice, You can do it as follow:

First, declare your protocol:

protocol SomeProtocol {
    static func someMethod()
}

Then, in your class you’ve 2 options:

First:

class SomeClass : SomeProtocol {
    class func someMethod()
}

Second:

class SomeClass : SomeProtocol {
    static func someMethod()
}

I hope, this may clarify your doubt..

Leave a Comment