Search code examples
iosobjective-cmacrosc-preprocessorconditional-compilation

What's the difference between #if and #ifdef Objective-C preprocessor macro?


How to define preprocessor macros in build settings, like IPAD_BUILD, and IPHONE_BUILD (and how to use them in my factory methods)?

I'm using these by heart now, would be cool to know what is going behind.


Solution

  • /#if works as usual if:

    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
      if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        return YES;
      }
    #endif
      return NO;
    }
    

    /#ifdef means "if defined - some value or macros":

    #ifdef    RKL_APPEND_TO_ICU_FUNCTIONS
    #define RKL_ICU_FUNCTION_APPEND(x) _RKL_CONCAT(x, RKL_APPEND_TO_ICU_FUNCTIONS)
    #else  // RKL_APPEND_TO_ICU_FUNCTIONS
    #define RKL_ICU_FUNCTION_APPEND(x) x
    #endif // RKL_APPEND_TO_ICU_FUNCTIONS
    

    or:

    #ifdef __OBJC__
        #import <Foundation/Foundation.h>
    #endif
    

    Use this link for more information http://www.techotopia.com/index.php/Using_Objective-C_Preprocessor_Directives

    To test whether you running iPad or not you should have smth like this:

    #define USING_IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
    
    if (USING_IPAD) {
        NSLog(@"running iPad");
    }
    

    Here's another useful preprocessor functions:

    #ifdef DEBUG
        //here we run application through xcode (either simulator or device). You usually place some test code here (e.g. hardcoded login-passwords)
    #else
        //this is a real application downloaded from appStore
    #endif