Protocol Inheritance + Delegates in Swift

I have a class with a delegate. I am creating a subclass that also has a delegate. I wanted the protocol used for the second delegate to extend the protocol used for the first delegate:

protocol MySuperClassProtocol { func foo() } class MySuperClass { var delegate:MySuperClassProtocol? } protocol MySubClassProtocol:MySuperClassProtocol { func bar() } class MySubClass: MySuperClass { override var delegate:MySubClassProtocol? // compiler error - "cannot override..." func test() { delegate?.foo() delegate?.bar() } } class UserClass:MySubClassProtocol { func foo() { println("foo!") } func bar() { println("bar") } } 

Is there any way to solve this? The only possible solution that I see is to make the 2 protocols independent of each other and use different names. Like this:

 protocol MySuperClassProtocol { func foo() } class MySuperClass { var mySuperClassDelegate:MySuperClassProtocol? } protocol MySubClassProtocol { func bar() } class MySubClass: MySuperClass { var mySubClassDelegate:MySubClassProtocol? func test() { mySuperClassDelegate?.foo() mySubClassDelegate?.bar() } } class UserClass:MySuperClassProtocol, MySubClassProtocol { func foo() { println("foo!") } func bar() { println("bar") } } 

But it looks a little strange + will not allow me to use the delegate naming convention - "delegate".

+7
ios swift
source share
3 answers

I tried to find the perfect solution for this for some time, but could not come up with anything better than this:

 protocol BaseDelegateProtocol: class { } class BaseDelegate: BaseDelegateProtocol { } class BaseActor { weak var delegate: BaseDelegate? = nil } // MARK: - protocol ConcreteDelegateProtocol: class { func doSomething() } class ConcreteDelegate: BaseDelegate, ConcreteDelegateProtocol { func doSomething() { // Do something } } class ConcreteActor: BaseActor { private weak var concreteDelegate: ConcreteDelegateProtocol? = nil override var delegate: BaseDelegate? { didSet { concreteDelegate = delegate as? ConcreteDelegateProtocol } } } 

Above works in Xcode 7 / Swift 2.

  • This template allows you to receive more and more protocols on the way down, inheriting from BaseDelegate .
  • There is no need to inherit protocols from each other, which helps isolate things.
  • didSet observer in the delegate property is automatically called for superclasses, so you do not need to explicitly call super.<blah> , and don’t forget about it
  • Specific delegate properties can remain confidential at every level of inheritance, thereby reducing clutter.
+5
source share

Sorry for the necroposting, the only thing I found:

 protocol SuperClassDelegate { func first_method() } class SuperClass { var delegate: SuperClassDelegate? func do_something() { delegate?.first_method() } } protocol SubClassDelegate: SuperClassDelegate { func second_method() } class SubClass: SuperClass { private var subDelegate: SubClassDelegate? override var delegate: SuperClassDelegate? { get { return self.subDelegate } set { self.subDelegate = newValue as! SubClassDelegate? } } //override func do_something() { // super.do_something() // subDelegate?.second_method() //} func do_something_other() { //subDelegate?.first_method() self.do_something() subDelegate?.second_method() } } class InheritanceAndDelegation: SubClassDelegate { let obj = SubClass() init() { obj.delegate = self } internal func first_method() { print("Hello from SuperClass") } internal func second_method() { print("Hello from SubClass") } func check() { obj.do_something_other() } } let inheritanceAndDelegation = InheritanceAndDelegation() inheritanceAndDelegation.check() //prints: //Hello from SuperClass //Hello from SubClass 

Commented code works too. Hope this will be helpful to someone.

+1
source share

You can do it differently, you can add the delegate variable to a subclass and use it to access SuperClassProtocol , also using delegate?.foo() .

 protocol MySuperClassProtocol { func foo() } class MySuperClass { //var delegate:MySuperClassProtocol? } protocol MySubClassProtocol:MySuperClassProtocol { func bar() } class MySubClass: MySuperClass { var delegate:MySubClassProtocol? func test() { delegate?.foo() delegate?.bar() } } class UserClass:MySubClassProtocol { func foo() { println("foo!") } func bar() { println("bar") } } 

But the problem with this approach is that you can never use MySuperClassProtocol independently unless you create a new SubClass from MySuperClass just to declare a delegate variable.

0
source share

All Articles