I am stuck on a protocol inheritance problem in Swift. I am trying to construct an API that accepts an array of Protocol Types X. Consumers of this API should be able to then pass in any type of protocol type X OR any sub protocol type X' that inherits from protocol X. It seems this can-not be achieved in Swift without some form of explicitly casting the Protocol type X' back to Protocol type X. Why is this the case? I dont see this behavior with Class Types (I suspect maybe its because the inheritance works markedly differently).
This is my code:
public protocol InjectableService
{}
public protocol ClientDependency
{
func serviceDependencies() -> [InjectableService.Type]
func injectDependencies(dependencies: [InjectableService])
}
public protocol TouchIdServiceInterface : InjectableService
{
func biometricAuthenticate() -> Void
}
public protocol PasswordServiceInterface : InjectableService
{
func passwordAuthenticate() -> Void
}
public class LoginController : ClientDependency
{
private var services: [InjectableService]!
public func injectDependencies(dependencies: [InjectableService]) {
services = dependencies
}
public func serviceDependencies() -> [InjectableService.Type] {
return [TouchIdServiceInterface.self as! InjectableService.Type , PasswordServiceInterface.self as! InjectableService.Type]
}
}
As you can see from above in the function serviceDependencies() I have to do this weird casting of the sub protocol type back to its super protocol type. If I remove the cast I get a compiler warning explicitly asking me to do that.
Is there any way to avoid this? I like that with class types it can figure it out for you. I really want to avoid this casting problem because its going to make using the API extremely clunky. Thanks!
From Apple document:
If you want to return
[InjectableService.Type]
, you need to define classes of each protocol.UPDATED
LoginController:
Testing: