user4788711
user4788711

Reputation:

Class Function to a Protocol using Extension in Swift?

I have a protocol SomeProtocol which should get a class function.

class func doSomething() -> Bool { ... }

I want to use an extension:

extension SomeProtocol { ... }

to add a class function.

Is it possible to extend a protocol with a class function?

Upvotes: 1

Views: 202

Answers (2)

Salihcyilmaz
Salihcyilmaz

Reputation: 326

Protocols can not be extended

You can create new protocol, that inherits from your protocol.

That is, if some type is going to conform to ExtendedProtocol, it has to implement SomeProtocol also

Protocol ExtendedProtocol: SomeProtocol {

class func doSomething ( ) -> Bool { ... }

}

Upvotes: 0

Stefan Salatic
Stefan Salatic

Reputation: 4513

You have extend the class with your protocol, so something like:

extension MyClass: SomeProtocol { ... }

Then you would implement that class func in that extension which conforms to SomeProtocol.

Upvotes: 1

Related Questions