The reason the first syntax example works is understandable if you think that any conditional statement can omit the { ... } block, as a result of which only the next statement is executed.
For instance:
if (something == YES) NSLog(@"Something is yes");
equivalently
if (something == YES) { NSLog(@"Something is yes"); }
The @autoreleasepool { ... } block is simply the next statement following the conditional expression.
Personally, I use the second syntax, because it is less prone to errors when making changes, and it becomes easier for me to read. Imagine that when you add an expression between the conditional and the @autoreleasepool { ... } block, the result is significantly different from the original. See This Naive Example ...
int i = 1; while (i <= 10) @autoreleasepool { NSLog(@"Iteration %d", i); ++i; }
Print "Iteration 1" through "Iteration 10". But:
int i = 1; int total = 0; while (i <= 10) total += i; @autoreleasepool { NSLog(@"Iteration %d", i); ++i; }
Actually it will cause an infinite loop because the ++i operator will never be reached, since it is syntactically equivalent:
int i = 1; int total = 0; while (i <= 10) { total += i; } @autoreleasepool { NSLog(@"Iteration %d", i); ++i; }
source share