回答

44

/#如果工作像往常一样,如果:

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

/#ifdef的意思是 “如果定义 - 一些价值或宏”:

#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 

或:

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

使用此链接更多信息 http://www.techotopia.com/index.php/Using_Objective-C_Preprocessor_Directives

测试是否正在运行的iPad或你不应该有水木清华这样的:

#define USING_IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad 

if (USING_IPAD) { 
    NSLog(@"running iPad"); 
} 

这里的另一个有用的预处理功能:

#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 
+0

感谢这只会编译下面的代码。可以肯定的是:是否定义了#define IPAD_BUILD(没有任何值?)在这种情况下,#ifdef IPAD_BUILD会返回true吗? – Geri

+0

似乎是的._____ – Geri

+0

实际上没有=)我会改变答案。 – Stas

9

一个宏可以是不确定的,也可以没有价值定义,或者它可以用一些值定义,可能是一个数字。实例:

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

的#ifdef MACRO或定义的#if(MACRO)检查宏是否定义,有或没有价值。

#if MACRO替代宏定义;如果宏没有被定义,那么它会替换为0,然后评估它找到的表达式。如果我们把上面的五个例子,#如果宏将变成

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

2号和3给出一个编译时错误。数字1和4的计算结果为false,所以下面的代码被跳过。数字5评估为真。

#如果是更灵活:你可以写

#if MACRO == 2 

如果宏被例如定义为

#define MACRO 2