How to debug ‘unrecognized selector send to instance’

How to debug ‘unrecognized selector send to instance’

In most of the cases Xcode do not take us to the exact line where this issue happen. When app crash you won?t see the line of code that caused this rather you will be taken to App delegate class, with error output something following:

[UITableViewCellContentView image]: unrecognized selector sent to instanceor [__NSDictionaryI objectAtIndex:] unrecognized selector sent to instanceor*** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[TestApp.MyViewController viewDidLoad:]: unrecognized selector sent to instance 0xDABCDD’

How to find line of code causing this:

Go to breakpoint navigator. Click ?+? option. click ?Exception Breakpoint?. An new widget like following will apear.

Image for post

Add following condition block:

-[NSObject(NSObject) doesNotRecognizeSelector:]

Image for post

*you can also put breakpoint for all exception.

Now run your code again. this time, breakpoint will trigger when this exception occurs.

Debug the issue:

  • Check if selector is called on right object. check if target you forget to set ?self? while setting the selector.

[self addTarget:self action:@selector(clickedOnElement) forControlEvents:UIControlEventTouchUpInside];

  • Check the function name for Typo.
  • Check the function parameters by printing it like following for signature mismatch:

Swift:print(#function)Obj:NSLog(@”%s”, __PRETTY_FUNCTION__)


No Responses

Write a response