如何定义预处理器宏来检查iOS版本

Bar*_*cki 6 iphone objective-c ios c-preprocessor

我用它来检查iOS版本,但它不起作用:

#ifndef kCFCoreFoundationVersionNumber_iPhoneOS_5_0
#define kCFCoreFoundationVersionNumber_iPhoneOS_5_0 675.000000
#endif

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_5_0
#define IF_IOS5_OR_GREATER(...) \
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iPhoneOS_5_0) \
{ \
__VA_ARGS__ \
}
#else
#define IF_IOS5_OR_GREATER 0
#endif
Run Code Online (Sandbox Code Playgroud)

我做的时候

#if IF_IOS5_OR_GREATER
NSLog(@"iOS5");
#endif
Run Code Online (Sandbox Code Playgroud)

什么都没发生.这里有问题吗?

Gow*_*iem 14

更简单:

#define IS_IOS6_AND_UP ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)
Run Code Online (Sandbox Code Playgroud)


Joe*_*ply 9

#ifdef __IPHONE_5_0 
Run Code Online (Sandbox Code Playgroud)

等等

只是寻找那个常数.所有目标c常量都以两个下划线开头


Jam*_*mes 5

定义此方法:

+(BOOL)iOS_5 {
    NSString *osVersion = @"5.0";
    NSString *currOsVersion = [[UIDevice currentDevice] systemVersion];
    return [currOsVersion compare:osVersion options:NSNumericSearch] == NSOrderedAscending;
}
Run Code Online (Sandbox Code Playgroud)

然后将宏定义为该方法.


Caj*_*uke 5

您已经定义了一个宏,但是您以非宏方式使用它.尝试这样的东西,使用相同的宏定义.

IF_IOS5_OR_GREATER(NSLog(@"iOS5");)
Run Code Online (Sandbox Code Playgroud)

(这不是你的#if/ #endifblock.)