I have a method in Swift with a signature of the form:
func myMethod<T>(class: T.Type) where T: SomeClass & MyProtocol
I would like to have a variable that is an array of classes that are all subclasses of SomeClass and conform to MyProtocol.
It would look something like:
let classArray = [SubclassOfSomeClass.self, SubclassTwoOfSomeClass.self, SubclassThreeOfSomeClass.self]
Where each of SubclassOfSomeClass, SubclassTwoOfSomeClass and SubclassThreeOfSomeClass conform to MyProtocol
I would then like to call myMethod as follows:
classArray.forEach { classType in
myMethod(class: classType)
}
I receive the compiler error "Generic parameter T could not be inferred". This makes sense to me as the type of classArray variable is inferred as [MyClass.Type].
My question is, can and how do I define the variable classArray such that the compiler knows that every type defined therein conforms to the protocol MyProtocol.