5

Given there are 2 Protocols P1 and P2, 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
MeXx
  • 3,357
  • 24
  • 39
  • 2
    Please read [this thread](http://stackoverflow.com/questions/26401778/in-swift-how-can-i-declare-a-variable-of-a-specific-type-that-conforms-to-one-o). It was first posted in the Swift 1 era, and the issue is still there in the coming Swift 3 era. Swift still does not have the ability express such types. – OOPer Jul 06 '16 at 10:36
  • The point that is made by Swift is that you don't need to do that if you design your architecture well. – Sulthan Jul 06 '16 at 10:52
  • @OOPer thanks, the example there is very similar to what I was trying to achieve. – MeXx Jul 06 '16 at 11:07

1 Answers1

0

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.

Image

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.


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

Alexander Zimin
  • 1,700
  • 1
  • 15
  • 19