MeXx MeXx - 1 year ago 115
Swift Question

Specify a class that implements a protocol

Given there are 2 Protocols

, it is possible to specify a type that conforms to both protocols, eg:

typealias P = protocol<P1, P2>

Is there a similar way to specify a type that is kind of a class and also conforms to a protocol, e.g. something like this (which does not work):

typealias P = UIView: P1

Answer Source

Unfortunately it's imposible in Swift 2.2 and won't be added in Swift 3.0. The idea that you want to create Type rule for types that inherit some class and implement protocol.


So it's not very common that you will have such hierarchy and will have property where you want to store one of this classes. Also in POP paradigm you should have additional protocol that give you properties that you need from UIView (described in answer above by @dfri).

Maybe you have such usecase: enter image description here

Then create additional class: enter image description here

And your type will be P1Base

P.S. That's why if you create @property (nonatomic) UIViewController<UITableViewDelegate> *protocolClassProperty; in Obj-c, it will be bridged as UIViewController! in Swift

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download