How would I declare a variable that is of a specific base class and conforms to a protocol?
For example if I have
class BaseClass { ... }
protocol SomeProtocol { ... }I can either declare it as
var someVar: BaseClassor as
var someVar: SomeProtocolbut not as
var someVar: BaseClass, SomeProtocolI also tried to declare the protocol as
protocol SomeProtocol: BaseClassBut of course this didn't work, as non-class types can't inherit from classes
In Objective-C I would do
BaseClass<SomeProtocol> *someVar;But in Swift <> is reserved for generics.
I can do this for function parameters and it works just as expected:
func someFunc<T: BaseClass where T: protocol<SomeProtocol>>(param: T)I could turn my protocol into a subclass of the BaseClass, but what if down the road I need some other base class, conforming to the protocol?
My concrete use case is that for a custom keyboard I have a controller that loads a bunch of different UIView subclasses (for different types of keyboards) that are constructed from different NIBs. They have custom functionality (abstracted in mini controllers of their own), but a common interface. If I declare them as variables conforming to a protocol, I would not be able to naturally use their UIView functionality without casting. I also won't get type safety when assigning/reading them. Same if I declare them as plain UIViews and cast.