Advertisements

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
#else
	NSLog(@"Not running in OS X");
#endif

This can also be used for debugging:

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

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:

#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
#define NSMaximumStringLength     (INT_MAX - 1)
#endif

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
else
    //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).

Advertisements
Leave a comment

4 Comments

  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

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

      Reply
      • 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

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

        Reply
  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

Advertisements
  • 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 222 other followers

  • Back to the Past

    March 2011
    S M T W T F S
    « Feb   Apr »
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
  • Time Machine

  • You count!

    • 621,662 views
  • Worldwide Stats

    free counters
  • Advertisements
%d bloggers like this: