Suppose I want to create a property which is a subclass of UIViewController and also conformant to the protocol MyDelegateProtocol. In Objective-C I would write something like:
@property (strong, nonatomic) UIViewController<MyDelegateProtocol> *delegate;
However, I'm not sure how to write this in Swift. I know how to declare a property which is protocol-conformant, or a property which is of a particular type:
let delegate : MyDelegateProtocol?
let delegate : UIViewController?
But I can't quite figure out how to make it do both. If I try something like:
let delegate : UIViewController<MyDelegateProtocol> ?
Then I get a compiler error about Cannot specialize non-generic type 'UIViewController'
. Probably because I'm wandering into the land of generics now. I've tried looking through the Swift book on protocols and other Stack Overflow questions regarding protocols, but I haven't found quite what I'm looking for.
A protocol can require any conforming type to provide an instance property or type property with a particular name and type. The protocol doesn't specify whether the property should be a stored property or a computed property—it only specifies the required property name and type.
The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. But there would be a time when you want to restrict protocols to be adopted by a specific class. In Swift 5, you can do just that.
Property requirements are always declared as variable properties, prefixed with the var keyword. Gettable and settable properties are indicated by writing { get set } after their type declaration, and gettable properties are indicated by writing { get } . var height: Int {return 5} // error!
You can create objects from classes, whereas protocols are just type definitions. Try to think of protocols as being abstract definitions, whereas classes and structs are real things you can create.
First of all, I think this is a code smell. If you want a delegate to be multiple things, there is most likely a separation of concerns problem.
With that said, if you still want to do this, there isn't a way in Swift. You have a few options though:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With