Created
January 28, 2021 07:38
-
-
Save tarunon/0165137d25742ec34ca9dc8805ca6c0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
private class Weakbox { | |
weak var object: NSObjectProtocol? | |
init(_ object: NSObjectProtocol) { | |
self.object = object | |
} | |
} | |
class DelegateForwarder: NSObject { | |
private var delegates = [Selector: Weakbox]() | |
func setDelegate(delegate: NSObjectProtocol?, for selectors: [Selector]) { | |
if let delegate = delegate { | |
selectors.forEach { selector in | |
self.delegates[selector] = Weakbox(delegate) | |
} | |
} else { | |
selectors.forEach { selector in | |
self.delegates.removeValue(forKey: selector) | |
} | |
} | |
} | |
override func responds(to aSelector: Selector!) -> Bool { | |
return delegates[aSelector]?.object?.responds(to: aSelector) ?? super.responds(to: aSelector) | |
} | |
override func forwardingTarget(for aSelector: Selector!) -> Any? { | |
return delegates[aSelector]?.object ?? super.forwardingTarget(for: aSelector) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment