Extension 11: Conditional Compilation

Building off of last week’s Extension, there are a few more preprocessor directives that allow you to control which portions of a program are compiled.

The #ifdef, #endif, #else, and #ifndef Statements

Sometimes (and hopefully not often), a program relies on system- or platform-specific features that must be written differently across different platforms. For example, not all iOS devices can run iOS 4.x, so any program that uses 4.x features must check to see if the system can support that feature. Or an application for OS X can check for the architecture of the system, PowerPC or Intel, or the version of the OS.

The The #ifdef, #endif, and #else directives work just like the The #ifdef, #endif, #elseif…else construct; the #endif directive is used to end an #ifdef block, just like a curly brace ( } ) ends an if block.

Note that a definition such as

#define POWER_PC

is sufficient to pass the #ifdef test; no value actually has to be specified for the constant.

So how does it work?

#ifdef MAC_OS_X
#	define DATADIR ~/Documents
	NSLog(@"Not running in OS X");

This can also be used for debugging:

#ifdef DEBUG
	NSLog(@"Data array is %@", self.dataArray);

This makes the code (your debugging intentions) clearer. When the program is working properly, you can undefine DEBUG and the NSLogs will not even be compiled in.

The #ifndef statement is the exact opposite of #ifdef—the code that follows gets executed only if the value is not defined.

The #if and #elif Directives

These directives are similar to if()… and else if()…. For example, the following appears in NSString.h:

#define NSMaximumStringLength     (INT_MAX - 1)

The example is rather self explanatory.

The operator defined (something) can also be used in #if statements:

#if defined(DEBUG)
// Is equivalent to
#ifdef DEBUG

A common use of this is #if defined (DEBUG) && DEBUG, which will be true if DEBUG has been defined to a non-zero value.

The #undef Operator

The #undef operator undefines a name:

#undef DEBUG

Subsequent #ifdef DEBUG and #if defined(DEBUG) statements evaluate to false.

Determining iPads

Conditional compilation can be useful in determining if you’re running on an iPad or not. Apple provides a UI_USER_INTERFACE_IDIOM() macro that can perform the check for you:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    //iPad code
    //iPhone code

If you have extensive code changes or want to load a different set of resources for example, the iPad code won’t ever get compiled for the iPhone and vice versa, which could simplify the resulting binary (remember, only apps under 20MBs can be downloaded off a 3G connection. You want to make your app as small as reasonably possible).

Leave a comment


  1. Ratna

     /  February 13, 2012

    Hi Using this can we do like the below:
    I want frame of a button which should load on conditional compiling .
    That is in header file can I define like if the device is iPad return some frame otherwise return other frame

    • Yep, just use the macro detailed at the end of the post.

      • Ratna

         /  February 21, 2012

        I tried it but in .pch file I could not able to work it

        #define isIPhone (![[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] || [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)

        #ifdef isIPhone
        #define xis @”isIphone”
        //This will only be compiled if SOME_CONDITION is defined as a compiler flag

        #define xis @”isIpad”
        The above code also not working

  1. Learn Objective-C in 24 Days « Programming for iOS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

  • Welcome

    My goal is to make CupsOfCocoa into a beautiful source for beginners to the iPhone platform to get started. Subscribe below for more, and stay tuned!

  • Contact Me

    If you need to contact me for any reason, feel free to send me an email.
  • The Giving Spirit

    If you've found this site helpful, would you consider donating a little sum? Any amount is appreciated...Thanks so much!

  • Roadmap

  • Enter your email address to follow this blog and receive notifications of new posts by email.

    Join 220 other followers

  • Back to the Past

    March 2011
    S M T W T F S
  • Time Machine

  • You count!

  • Worldwide Stats

    free counters
%d bloggers like this: