Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Declare Class Func in Protocol

I had following confusion. As far as I know the main difference between static and class keywords when declaring method is that the second one could be overridden in subclasses.

The problem

However when I declare a protocol in Swift 1.2 like this:

protocol MyProtocol {     class func dummyClassMethod() } 

compiler gives an error:

Class methods are only allowed within classes; use 'static' to declare a static method

The error is pretty descriptive as obviously MyProtocol is not a class, however I want to make a class func part of the protocol.

What I've tried

I've found that if I declare interface in protocol as static, compiler is happy and I could use this static method in all classes that adopt this protocol:

protocol MyProtocol {     static func dummyClassMethod() } 

The question

So my question basically is is this right? This declaration states that my class method cannot be overridden in children, however in my implementation I could write and use the following:

class ClassA: MyProtocol {     class func dummyClassMethod() {      } }  class ClassB: ClassA {     override class func dummyClassMethod() {      } } 

and now my dummyClassMethod is not static anymore...

  1. Compiler is Ok and everything works - but why?

  2. Is it specific to the fact that interface itself is static, however it's implementation is not?

  3. Is there a better alternative for class func in protocols?

Objective-C solution

In ObjC this is pretty easy and compile & run flawlessly:

@protocol MyProtocol   +(void)dummyClassMethod;  @end 
like image 450
hris.to Avatar asked May 14 '15 09:05

hris.to


People also ask

Can a protocol have a class method?

No, it's right. Protocols, even class-constrained ones, aren't classes. There's no such thing as a final func in a protocol, and naturally there's no such thing as a class func, because there's no inheritance.

CAN protocol inherit from class Swift?

Protocols allow you to group similar methods, functions, and properties. Swift lets you specify these interface guarantees on class , struct , and enum types. Only class types can use base classes and inheritance from a protocol.

Can Swift protocols have properties?

Protocol InheritanceSwift 4 allows protocols to inherit properties from its defined properties. It is similar to that of class inheritance, but with the choice of listing multiple inherited protocols separated by commas.

Can we declare variables in protocol?

The protocol doesn't specify whether the property should be a stored property or a computed property—it only specifies the required property name and type. Property requirements are always declared as variable properties, prefixed with the var keyword.


2 Answers

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

like image 124
eMdOS Avatar answered Sep 22 '22 19:09

eMdOS


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

like image 24
Ilia Avatar answered Sep 22 '22 19:09

Ilia