I can't think of a good way to express this in Swift. The syntax for a type is:
type → array-type | dictionary-type | function-type | type-identifier |
tuple-type | optional-type | implicitly-unwrapped-optional-type |
protocol-composition-type | metatype-type
What you're looking for would be a kind of protocol-combination-type that also accepts a base class. (Protocol-combination-type is protocol<Proto1, Proto2, Proto3, …>
.) However, this is not currently possible.
Protocols with associated type requirements are allowed to have typealiases that specify a required base class and required protocols, but these also require the types to be known at compile-time, so it's unlikely to be a viable option for you.
If you're really into it, you can define a protocol with the parts of the interface of UIViewController
that you use, use an extension to add conformance, and then use protocol<UIViewControllerProtocol, CustomProtocol>
.
protocol UIViewControllerProtocol {
func isViewLoaded() -> Bool
var title: String? { get set }
// any other interesting property/method
}
extension UIViewController: UIViewControllerProtocol {}
class MyClass {
var controller: protocol<UIViewControllerProtocol, CustomProtocol>
}