Cannot call a type method in a class that matches the protocol

My code is very simple.

My protocol:

protocol BaseEntity { class func getInstance(sourceObject: [NSObject : AnyObject], context: AnyObject!) -> BaseEntity init() } 

Then in another class I have this method:

 private func convertJSONDictionaryIntoModel(jsonDictionary : [NSObject : AnyObject], mapClass: BaseEntity.Type) -> BaseEntity { let object = mapClass.getInstance(jsonDictionary, context: nil) return object } 

I got a compiler error in the first line, which reads as "Access to protocol type members. BaseEntity.Type not implemented"

As far as I can tell, this is possible in Swift.

+5
source share
1 answer

I also ran into this! This means that the Swift programming language does not support class methods inside protocols ... for now. The workaround for this seems to depend on the specific situation, and there is no "you should do this instead." If anyone has a better solution, please share!

This also applies to class variables, however there is a hacky workaround for this .

0
source

Source: https://habr.com/ru/post/1208478/


All Articles