43

Is it possible to set the breakpoint in Xcode to have the debugger stop only on unrecognized selector?

I have other exceptions that are triggering, and I only want to trigger on the unrecognized selector exception, nothing else.

AWF4vk
  • 5,810
  • 3
  • 37
  • 70
  • 2
    You may know this already, but be sure you know that although exceptions can trigger when you're debugging a project, they should never trigger in shipping code. Unlike some other languages, Objective-C exceptions are considered programmer error and your code should definitely not be triggering them as part of the normal flow of the app. – Rob Keniger Aug 23 '11 at 04:43
  • @Rob I heard this before. But then read the docs on exception handling. They didn't really make them out to be such bad things. So I should rely on explicitly returning values of problems? Just curious, why's that? – AWF4vk Aug 23 '11 at 12:53
  • Have a look at [the docs](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Exceptions/Exceptions.html), in particular the big box labelled "Important". You should use the [`NSError` handling pattern](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ErrorHandlingCocoa/ErrorHandling/ErrorHandling.html#//apple_ref/doc/uid/TP40001806) instead. One of the main reasons is that exceptions are very expensive operations in the Objective-C runtime. – Rob Keniger Aug 23 '11 at 13:22

1 Answers1

82

Setting a symbolic breakpoint for -[NSObject doesNotRecognizeSelector:] should do the trick.

In Xcode 4, press cmd+6 to go to the Breakpoint Navigator, then click the +and choose "Add Symbolic Breakpoint…". Configure.

justin
  • 104,054
  • 14
  • 179
  • 226
  • 6
    Be sure to enter in the entire `-[NSObject doesNotRecognizeSelector:]` or it won't work and you will be confused (as I was.) – zekel Jul 05 '12 at 20:39
  • @zekel what do you mean with enter in the entire `-[NSObject doesNotRecognizeSelector:]`? – Peter Warbo Sep 11 '12 at 12:11
  • 2
    @PeterWarbo I just meant that you should literally enter this entire string `-[NSObject doesNotRecognizeSelector:]` , not just the selector `doesNotRecognizeSelector:`, etc. – zekel Sep 11 '12 at 14:41
  • 1
    this definitely worked for me! my bug was subtle: wrote ````@selector(foo)```` instead of ````@selector(foo:)```` – rbp Sep 01 '13 at 16:31
  • It is nicely [described here](http://www.fruitstandsoftware.com/blog/2012/08/quick-and-easy-debugging-of-unrecognized-selector-sent-to-instance/) – Lukasz 'Severiaan' Grela Mar 27 '14 at 12:12