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

时间:2022-06-16 07:00:18

I use it to check iOS version, but it doesn't work:

我用它来检查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

when I make

当我做

#if IF_IOS5_OR_GREATER
NSLog(@"iOS5");
#endif

nothing happens. Is something wrong here?

什么也不会发生。是错误的吗?

6 个解决方案

#1


5  

You've defined a macro, but you're using it in the non-macro way. Try something like this, with your same macro definition.

你已经定义了一个宏,但是你用的是非宏的方式。试试这样的东西,用相同的宏定义。

IF_IOS5_OR_GREATER(NSLog(@"iOS5");)

(This is instead of your #if/#endif block.)

(这不是你的#if/#endif块。)

#2


13  

Much simpler:

简单得多:

#define IS_IOS6_AND_UP ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)

#3


8  

#ifdef __IPHONE_5_0 

etc

Just look for that constant. All the objective c constants start with two underscores

只要找这个常数。所有目标c常量从两个下划线开始

#4


5  

Define this method:

定义这个方法:

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

Then define the macro as that method.

然后将宏定义为该方法。

#5


2  

For a runtime check use something like this:

对于运行时检查,请使用以下内容:

- (BOOL)iOSVersionIsAtLeast:(NSString*)version {
    NSComparisonResult result = [[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch];
    return (result == NSOrderedDescending || result == NSOrderedSame);
}

If you create a category on UIDevice for it, you can use it as such:

如果您在UIDevice上为它创建一个类别,您可以这样使用它:

@implementation UIDevice (OSVersion)
- (BOOL)iOSVersionIsAtLeast:(NSString*)version {
    NSComparisonResult result = [[self systemVersion] compare:version options:NSNumericSearch];
    return (result == NSOrderedDescending || result == NSOrderedSame);
}
@end

...

if([[UIDevice currentDevice] iOSVersionIsAtLeast:@"6.0"]) self.navigationBar.shadowImage = [UIImage new];

#6


1  

#define isIOS7 ([[[UIDevice currentDevice]systemVersion]floatValue] > 6.9) ?1 :0

#1


5  

You've defined a macro, but you're using it in the non-macro way. Try something like this, with your same macro definition.

你已经定义了一个宏,但是你用的是非宏的方式。试试这样的东西,用相同的宏定义。

IF_IOS5_OR_GREATER(NSLog(@"iOS5");)

(This is instead of your #if/#endif block.)

(这不是你的#if/#endif块。)

#2


13  

Much simpler:

简单得多:

#define IS_IOS6_AND_UP ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)

#3


8  

#ifdef __IPHONE_5_0 

etc

Just look for that constant. All the objective c constants start with two underscores

只要找这个常数。所有目标c常量从两个下划线开始

#4


5  

Define this method:

定义这个方法:

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

Then define the macro as that method.

然后将宏定义为该方法。

#5


2  

For a runtime check use something like this:

对于运行时检查,请使用以下内容:

- (BOOL)iOSVersionIsAtLeast:(NSString*)version {
    NSComparisonResult result = [[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch];
    return (result == NSOrderedDescending || result == NSOrderedSame);
}

If you create a category on UIDevice for it, you can use it as such:

如果您在UIDevice上为它创建一个类别,您可以这样使用它:

@implementation UIDevice (OSVersion)
- (BOOL)iOSVersionIsAtLeast:(NSString*)version {
    NSComparisonResult result = [[self systemVersion] compare:version options:NSNumericSearch];
    return (result == NSOrderedDescending || result == NSOrderedSame);
}
@end

...

if([[UIDevice currentDevice] iOSVersionIsAtLeast:@"6.0"]) self.navigationBar.shadowImage = [UIImage new];

#6


1  

#define isIOS7 ([[[UIDevice currentDevice]systemVersion]floatValue] > 6.9) ?1 :0