I am debugging a library that implements a UIScrollViewDelegate
proxy (basically) like:
class ScrollViewDelegateProxy: NSObject, UIScrollViewDelegate {
personal weak var realDelegate: UIScrollViewDelegate?
init(scrollView: UIScrollView) {
tremendous.init()
self.realDelegate = scrollView.delegate
scrollView.delegate = self
}
// MARK: - UIScrollViewDelegate
// Different scroll delegate strategies...
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// Do one thing...
realDelegate?.scrollViewDidScroll?(scrollView)
}
// MARK: - Methodology Forwarding
override func responds(to aSelector: Selector!) -> Bool {
if let realDelegate {
realDelegate.responds(to: aSelector)
} else {
tremendous.responds(to: aSelector)
}
}
override func forwardingTarget(for aSelector: Selector!) -> Any? {
if let realDelegate, realDelegate.responds(to: aSelector) {
realDelegate
} else {
tremendous.forwardingTarget(for: aSelector)
}
}
}
The strategy forwarding is carried out in case the proxy is distributed messages exterior of UIScrollViewDelegate
. E.g. if the scrollView have been a UITableView
, then the delegate
property could be a UITableViewDelegate
with extra strategies that aren’t be straight supported by this proxy. In that case, it ought to simply move it on.
As a result of my use case is a UITableView
. I’ve discovered that this may crash (not at all times) with the error message:
'NSInvalidArgumentException', motive: '-[SomeLibrary.ScrollViewDelegateProxy tableView:viewForHeaderInSection:]: unrecognized selector despatched to occasion'
I assume it’s because:
responds(to:)
returnstrue
for a selector whereas therealDelegate
is in reminiscencerealDelegate
deallocatesforwarding goal(for:)
is known as later with that selector, however it isn’t carried out on the proxy- The invocation causes a crash
I noticed this weblog publish however wasn’t certain what the Swift equal was.
Is utilizing a weak
property like this with technique forwarding merely unsafe?
And what would possibly a protected implementation appear like?