0

I have this delegate that conforms to SomeProtocol

weak var delegate:SomeProtocol?

Now I want this delegate to be a subclass of SomeClass.

How can I do that in one declaration?

Do I have to define a new subclass that both inherits from SomeClass and conforms to SomeProtocol?

pableiros
  • 14,932
  • 12
  • 99
  • 105
etayluz
  • 15,920
  • 23
  • 106
  • 151
  • 2
    Note that there are several similar questions already, for example http://stackoverflow.com/questions/29881621/make-property-of-type-and-also-conform-to-protocol-in-swift – Martin R Aug 26 '16 at 17:00
  • We should mark this as duplicate or delete it then – etayluz Aug 26 '16 at 17:01

2 Answers2

0

In Swift you can make your class inherit from another class and conform to several protocols.

Here's the syntax

protocol CanRun { }
class Animal { }

class Cat: Animal, CanRun { }
Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
0

A variable can have just one class type or conform many protocols, in your case you can create another protocol that SomeClass conform, let say SecondProtocol and specify that your delegate conform both SomeProtocol and SecondProtocol:

weak var delegate: protocol<SomeProtocol, SecondProtocol>

or do what you said about the new subclass.

Jans
  • 11,064
  • 3
  • 37
  • 45
  • Is there a way in one line to declare a variable of one class type and inherits from one protocol? – etayluz Aug 26 '16 at 16:29
  • 1
    No, there's no way to do things like *var delegate:SomeClass,protocol* if it's what you want. – Jans Aug 26 '16 at 16:36