Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a property of a particular class which is also protocol conformant?

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.

like image 592
UberJason Avatar asked Aug 07 '14 00:08

UberJason


People also ask

Can Swift protocols have properties?

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.

Can we make protocol only class specific struct can not adopt it?

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.

How do you use a variable in protocol?

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!

What's the difference between a protocol and a class in Swift?

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.


1 Answers

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:

  1. Add the required UIViewController methods to your delegate protocol (my favorite option)
  2. Create a superclass for your view controller that is a subclass of UIViewController and implements MyDelegateProtocol. Declare your variable as that type
  3. Store two references to your "delegate". One is of the MyDelegateProtocol and the other is of UIViewController
  4. Make your class generic, but this will not be accessible from Objective-C
like image 56
drewag Avatar answered Oct 21 '22 12:10

drewag