Reading static var from protocol extension instance method

Say we have a Swift protocol:

protocol SomeProtocol: class { static var someString: String { get } } 

Is there a way to access someString from an extension instance method, for example?

 extension SomeProtocol { public func doSomething() -> String { return "I'm a \(someString)" } } 

I get a compiler error:

The static member "someString" cannot be used in an instance of type "Self"

Is there any way to do this?

+6
source share
1 answer

You need to access someString using Self (note the uppercase S ):

 extension SomeProtocol { public func doSomething() -> String { return "I'm a \(Self.someString)" } } 
+6
source

All Articles