ios – NSObject proxy with weak delegate crashes with NSInvalidArgumentException unrecognized selector

ios – NSObject proxy with weak delegate crashes with NSInvalidArgumentException unrecognized selector


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:) returns true for a selector whereas the realDelegate is in reminiscence
  • realDelegate deallocates
  • forwarding 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?

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
rooshohttps://www.roosho.com
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Latest Articles

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog.