-1

Given a very simple protocol :

protocol TheProtocol {
    func doSomething()
    func doSomethingElse()
    func doThis()
    func doThat()
}

I have a class Base that has a delegate waiting to be set.

// Can't modify this class at all
class Base  {
    public var delegate: TheProtocol?
}

My second class B inherits from this class Base, and implements TheProtocol in order to set the delegate to itself.

class B: Base, TheProtocol {

    override init() {
        super.init()
        self.delegate = self
    }

    func doSomething() {

    }
    func doSomethingElse() {

    }
    ... other methods to implement
}

Now what I want to be able to do, is to have a last class C, that contains an instance of B, and also set the delegate. I want the delegate to work both inside B and C.

The major constraint is that I can't modify the Base class.

class C: TheProtocol {

    var obj = B()

    init() {
        // If I do this it won't fire within B anymore
        obj.delegate = self
    }

    func doSomething() {

    }
    func doSomethingElse() {

    }
    ... other methods to implement
}
Scaraux
  • 3,841
  • 4
  • 40
  • 80
  • Class B has to be written to proxy the delegate so it can handle the call and call the protocol method on behalf of the "real" delegate. – rmaddy Dec 03 '18 at 21:24
  • @RobertDresler as I mentioned, I can't modify the first class and make a multicast, it's in the title – Scaraux Dec 03 '18 at 22:06
  • @rmaddy, so If my protocol has 25 methods, I need to override them all, do the work, and inside call my new delegate ? – Scaraux Dec 03 '18 at 22:09
  • That's one way. But obviously that would be a mess with 25 methods. I suggested it because you showed 1, not 25. – rmaddy Dec 03 '18 at 22:10
  • @rmaddy unfortunately I did that as a simplified example. In real, I am dealing with the UICollectionViewDelegate... – Scaraux Dec 03 '18 at 22:11
  • That would be good information to put in your question. – rmaddy Dec 03 '18 at 22:16
  • I want it to be implemented two times. So B intercepts messages, but C can also have the messages. – Scaraux Dec 03 '18 at 22:45
  • For example, I create a class `MyCollectionView` that overrides a `UICollectionView`. In this class, I set the `delegate` to `self` so I can access `scrollViewDidScroll()`. But, I also want to set `MyCollectionView`'s delegate from outside: I might want `scrollViewDidScroll()` from **outside** too. Does it make sense ? Therefore I need some proxy delegation, because I can't set the `delegate` from inside and outside. – Scaraux Dec 03 '18 at 22:51
  • Yes, this is what I meant. I can set it but obviously not make the use of two at the same time. I found some objc implementations of proxy delegate – Scaraux Dec 03 '18 at 22:55
  • I am trying to convert it to Swift, but not sure it'll work... Do you have any other idea for this ? – Scaraux Dec 03 '18 at 22:56

1 Answers1

0

It is actually possible using a Proxy delegate. However not really recommended.

In Swift, how do I have a UIScrollView subclass that has an internal and external delegate?

Scaraux
  • 3,841
  • 4
  • 40
  • 80