如何定义预处理器宏以检查 iOS 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7836967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to define preprocessor macro to check iOS version
提问by Bartosz Bialecki
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?
没发生什么事。这里有什么问题吗?
采纳答案by Cajunluke
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
块。)
回答by Gowiem
Much simpler:
更简单:
#define IS_IOS6_AND_UP ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)
回答by Joel Teply
#ifdef __IPHONE_5_0
etc
等等
Just look for that constant. All the objective c constants start with two underscores
只要寻找那个常数。所有目标 c 常量都以两个下划线开头
回答by James
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.
然后将宏定义为该方法。
回答by dankrusi
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];
回答by Sarthak Patel
#define isIOS7 ([[[UIDevice currentDevice]systemVersion]floatValue] > 6.9) ?1 :0