How can I debug 'unrecognized selector sent to instance' error
Try setting a symbolic breakpoint on -[NSObject(NSObject) doesNotRecognizeSelector:]
. Just click [+]
in the bottom left corner of the Breakpoint Navigator to add a breakpoint. Then click 'Add Symbolic Breakpoint'. Reproducing your crash now should give you a better idea where in your code the issue occurs.
You can easily track such crashes using Exception Breakpoints
.
Open the Breakpoint Navigator
and add the
Once you add the Exception Breakpoint, option will be open to choose the Exception
.
Select Objective-C
.
Run the code and crash the application, breakpoint will stop you to the point where the code is crashing.
The critical first step is to analyze the error message:
[UITableViewCellContentView image]: unrecognized selector sent to instance
This tells you that the "message" image
was "sent" to an object of class UITableViewCellContentView. (In other words, an attempt was made to call the method image
on an object of class UITableViewCellContentView.)
The first thing to ask is "Does this make any sense at all?" It may be that the named class has an Image
method, but not an image
method, and so the wrong method name was used on the call. Or it may be that the named method is someMethod:someParm:
, but the class implements someMethod:someParm:anotherParm:
, meaning that a parameter was omitted on the call.
Most often, though, the named class does not have any method even vaguely resembling the named method, meaning that somehow a pointer to the wrong object was used in the failing call.
For instance, one might do:
NSArray* myArray = [myDictionary objectForKey:@"values"];NSString* myString = [myArray objectAtIndex:5];
And get an error along the lines of:
[__NSDictionaryI objectAtIndex:] unrecognized selector sent to instance
because the object retrieved from myDictionary
was, in fact, an NSDictionary, not the NSArray that was expected.
Most confusing, unfortunately, is when this sort of error occurs deep in UI system code rather than in your own code. This can happen when you somehow passed the wrong object to a system interface, or perhaps configured the wrong class in Interface Builder or wherever.