Saturday, September 1, 2012

Debugging: unrecognized selector sent to instance

If you are a cocoa developer, you must be familiar with  "unrecognized selector sent to instance". And many number of times, it may have increased your caffeine intake :) . But in this post, we are going to see an elegant solution to pin point to the root cause of this message.

Suppose your console shows you the message:
-[MoneyWellAppDelegate doThatThingYouDo]: unrecognized selector sent to instance 0xa275230

Looking at the Xcode backtrace doesn't seem to help either, since it most likely looks like so:





At this point you start to hunt through your code, looking to see, who sent the -doThatThingYouDo message. You may find the answer right away or you may spend the next hour trying to figure out where that call is coming from.

The Elegant Way

The better way of finding out where the things have gone wrong is to pop over to the breakpoint navigator, click the + button at the bottom and choose Add Symbolic Breakpoint .

In the symbol field enter this symbol:

-[NSObject (NSObject) doesNotRecognizeSelector:]

Now when any instance of any object within your program is sent a message to which it does not respond, you will be presented with a backtrace that takes you right to the point where that message was sent.



So you see, easy peasy japanesey :)

CREDITS:

The original source of this blog post is here. The contents of this blog post have been published by the permission of the original author Michael Fey. Many thanks to Michael Fey for this wonderful post.

No comments:

Post a Comment