3

I opened snippet of the NSAssert and found this:

do {                \
    __PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
    if (!(condition)) {     \
         [[NSAssertionHandler currentHandler] handleFailureInMethod:_cmd \
         object:self file:[NSString stringWithUTF8String:__FILE__] \
         lineNumber:__LINE__ description:(desc), ##__VA_ARGS__]; \
      }             \
    __PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \
} while(0)

Why do ... while(0) was used? It will be allways performed one time.

rishi
  • 11,779
  • 4
  • 40
  • 59
skyylex
  • 855
  • 1
  • 10
  • 24
  • Good question. I saw a similar macro snippet where pragma params were pushed and it was also wrapped in `do { ... } while(0)`. Would like to read the answer to this. – Léo Natan Jul 12 '14 at 12:41
  • 2
    http://stackoverflow.com/questions/257418/do-while-0-what-is-it-good-for – Igor Jul 12 '14 at 12:45
  • 1
    It's a macro trick -- one of several ways to assure that if the macro will not accidentally interact with the program structure around it. Eg, if one coded `if (someTest) NSAssert(...) else doSomething;` the logic of that would be mucked up without the surrounding do/while. – Hot Licks Jul 12 '14 at 12:58

0 Answers0