I have a generic problem in Swift (3):
I get different data from different classes, implementing the same protocol, from the server, and I need to put them in a class with generics (e.g. Array).
I don't know what data class will be, so I need to use a protocol. Therefore, I have the following structure:
My protocol:
protocol MyProtocol {
}
Some classes that implement the protocol
class MyProtocolImpl1: MyProtocol{
}
class MyProtocolImpl2: MyProtocol {
}
....
class with common:
final class MyGenericsClass<T: MyProtocol> {
}
Now I want to use this class as follows:
func createClass<T>(model: T.Type) -> MyGenericClass<T> {
let myClass = MyGenericClass<T>()
return myClass
}
...
EDIT
func getClass() -> MyProtocol.Type {
return MyProtocolImpl1.self
}
let impl1 = getClass()
let impl2 = MyProtocolImpl2.self
let createdClass = createClass(impl1)
let createdClass = createClass(impl2)
Execution createClass(impl1)I get this error:
cannot invoke 'createClass' with an argument list of type '(MyProtocol.Type)'
Changing MyProtocol for the class would fix the problem, but then I couldnโt be sure that every class that inherits from it implements the necessary methods.
Does anyone have any ideas how to solve this problem?