Suppress Unused Variable Warnings With #pragma unused

May 25, 2012

In the interest of clean compiles, a warning about an unused variable is welcome. However, should you have a situation that calls for leaving an unused variable in the code, keep reading.

One example where I’ve found this handy is when I have a variable that is a placeholder for something that has yet to be implemented.

Here is how a typical warning looks for an unused variable.

To suppress the warning, add a #pragma as follows:

NSString *tempMsg;
#pragma unused (tempMsg)

The output in Xcode now looks as follows, notice there are no warnings:

You can add more than one variable to the #pragma statement as follows:

NSString *tempMsg;
int flag = 0;
char *ptr;
#pragma unused (tempMsg, flag, ptr)

One reason I find this approach helpful versus commenting out a variable(s), is that I can do a project search for “unused” and get a quick glimpse of code that I need to revisit.