iOS Dev Nugget 54 Suppressing Xcode's Unused Variable Warning


Need to run a code review on your codebase? Hire me

As I've written in previous tips – #7 and #12 – pragmas are useful for helping organize code and suppress certain compiler warnings. A warning you sometimes want to suppress is the “Unused Variable” warning. Sometimes you write code where you want to store the result in a local variable, but may not actually use the variable right now (perhaps it is only used in debugging code?).

BOOL result = [[NSFileManager defaultManager] createFileAtPath:aPathString contents:UIImageJPEGRepresentation(anImage, 0.7) attributes:nil];
#pragma unused(result)

The single line of code will trigger a Unused Variable warning with Xcode. By adding the #pragma after the line where the variable is declared, you will suppress the warning, including that you knowingly created the unused variable.

Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No


Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter


View archives of past issues