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

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


/#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 informationhttp://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() == UIUserInterfaceIdiomPadif (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


A macro can be undefined, it can be defined with no value, or it can be defined with some value, possibly a number. Examples:

#undef MACRO#define MACRO#define MACRO ??????#define MACRO 0#define MACRO 1

#ifdef MACRO or #if defined (MACRO) checks whether the macro is defined, with or without value.

#if MACRO substitutes the macro definition; if the macro is not defined then it substitutes 0. It then evaluates the expression that it find. If we take the five examples above, #if MACRO will be turned into

#if 0#if#if ??????#if 0#if 1

Number 2 and 3 give a compile time error. Number 1 and 4 evaluate to false, so the following code is skipped. Number 5 evaluates to true.

#if is more flexible: You could write

#if MACRO == 2

which will only compile the following code if the macro was defined for example as

#define MACRO 2