Swift Declare Class Func in Protocol Swift Declare Class Func in Protocol ios ios

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


https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol doesn’t actually provide an implementation for any of these requirements—it only describes what an implementation will look like. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements.

After this protocol definition it becomes reasonable that

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


To make protocol method static and final implement that method with static keyword

class ClassA: MyProtocol{    static func dummyClassMethod() {    }} 

and now you cant override dummyClassMethod function anymore. If you want to prevent overriding only you must declare protocol method as final. About class functions, they were not fully supported in Swift 1.0 and now in Swift 1.2 I think that they are moving towards static functions


matomo