In iOS source examples, you’ve probably come across the use of
#pragma. Pragmas are directives which instruct the compiler to use pragmatic or implementation-dependent features. Xcode supports some of helpful pragmas that can help you organize and manage your code.
#pragma mark A Heading
mark argument is probably the most common one you’ll see used in Xcode. When added to your code it will give you a nice heading in the navigation bar as shown here:
A mark with a dash (-) such as this:
#pragma mark - A Heading
will include a divider line above the heading. Leave the dash out and you’ll just get the heading. You can also do something like this:
#pragma mark - #pragma mark A Heading
and you’ll get the same thing.
You should keep you code clean and ideally without unused variable but placing a
#pragma with the
unused argument after a variable lets you suppress those “unused variable” warnings you see far too often.
BOOL test = YES; #pragma unused(test)
#pragma clang diagnostic
(thanks to Guy Shaviv from tvistudio.com for sending this in)
clang diagnostic is useful to get rid of those pesky warnings when performing a selector with ARC from a variable, and ARC doesn’t know if the selector returns a retained object or not.
#pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-performSelector-leaks" [obj performSelector:sel withObject:arg]; #pragma clang diagnostic pop
Some other directives
While I’ve got your attention there two other useful directives you might want to try:
#error. As you can guess, these generate warnings and error durring compile. Warning as especially useful for TODO items. I used to use a TODO comment to remind myself of TODO’s in the code:
// TODO fix this.
but more often than not they were forgotten until moths later when a bug poped up and the line above read ‘// TODO fix this’. Instead, I use a #warning so that all the TODO’s are in the compiler warnings:
#warning This is not complete
The #error directive does the same only it’s generates an error and halts the compile. This is useful in the case of a preprocessor conditional
#ifdef SOMETHING // Do stuff #else #error SOMETHING was not defined!!! #endif
If you know of any other neat #pragmas let me know and I’ll add them here.