7

The code shouldn't have any effect It should be doable anywhere It doesn't generate compiler warning

Basically sometimes I want to do NSAssert. However, rather than NSAssert, I sometimes want to do my if {} my self. That way I can just easily set or unset the break point

Currently this is what I do.

if (entityName==@"Business")
{
    error=nil; //Some code for breakpoint. I want something that do nothing actually.
}

It's just happen that I have a variable named error that I will no longer use. But what would be a goodalternative

I also tried [nil donothing]; but that got compiler error.

user4951
  • 32,206
  • 53
  • 172
  • 282

5 Answers5

8

Try this:

while (false);

There are other possibilities, like if (false) or even just a line with a lone semicolon ;, but in those cases execution stops at the next instruction, possibly because the compiler simply eliminates those empty bits of code. Using while has the advantage that execution will stop on that line (assuming you put a breakpoint there, of course).

Caleb
  • 124,013
  • 19
  • 183
  • 272
3

You can give breakpoints conditions, hold down option and command and click the breakpoint, you should get a pop-up. In the 'condition' field yo can enter something like

[entityName isEqualToString:@"Business"]

(Note that you should compare strings with -isEqualToString: not ==, as the latter compares the pointers not the characters in the strings themselves.)

By using the condition in the breakpoint you shouldn't need the if statement at all, and also enable and disable the check without without recompiling. There is a lot more you can do with the breakpoint settings, like have it automatically log a message or run a debugger command etc.

Vic Smith
  • 3,477
  • 1
  • 18
  • 29
  • I personally find having lots of debug conditions in my code to be a pain, as you rapidly lose track of where they all are anyway and then have to hunt them down for production, or just tracking down a different bug. Putting all the debug steps like breaking, logging, alerting incorrect code state etc I find much more convenient to be in xcode breakpoints. Then I can access a list of them, turn them on and off easily, and they don't get committed to source control unless explicitly made to. But everyone has there own way of coding and you should definitely use what you feel works best for you. – Vic Smith Sep 25 '12 at 10:00
  • Actually what you do make sense too. Just repeat the condition. Hmm... Source control. I got to learn how to use it. – user4951 Sep 25 '12 at 10:18
3

In C, you can have a no-op instruction simply putting a semicolon alone in a line:

if (TRUE) {
  ; // Neutral operation
} 

In Objective-C you could do the same, the thing is your program stops before the next line, as you can see here:

enter image description here

Federico Zancan
  • 4,846
  • 4
  • 44
  • 60
  • Excellent tip. I want to insert such a line with comment to make the code self-documenting that "No code needed at this point.". – Basil Bourque Dec 04 '12 at 04:36
2

you don't compare 2 strings directly; try [NSString isEqualToString:]

Elden
  • 636
  • 1
  • 6
  • 21
0

you can use __asm int 3;

if ([entityName isEqualToString:@"Business"]) {
    __asm int 3;
} 

From CFInternal.h.

#if defined(__ppc__) || defined(__ppc64__)
#define HALT asm __volatile__("trap")
#elif defined(__i386__) || defined(__x86_64__)
#if defined(__GNUC__)
#define HALT asm __volatile__("int3")
#elif defined(_MSC_VER)
#define HALT __asm int 3;
#else
#error Compiler not supported
#endif
#endif
if ([entityName isEqualToString:@"Business"]) {
    HALT;
} 
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144