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

发布于 2024-12-11 01:20:51 字数 525 浏览 0 评论 0原文

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

当我

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

什么也没发生时。这里有什么问题吗?

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

#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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

秋叶绚丽 2024-12-18 01:20:51

更简单:

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

Much simpler:

#define IS_IOS6_AND_UP ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)
夜声 2024-12-18 01:20:51
#ifdef __IPHONE_5_0 

等等

只要寻找那个常数即可。所有目标 c 常量都以两个下划线开头

#ifdef __IPHONE_5_0 

etc

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

把人绕傻吧 2024-12-18 01:20:51

您已经定义了一个宏,但您以非宏方式使用它。使用相同的宏定义尝试类似的操作。

IF_IOS5_OR_GREATER(NSLog(@"iOS5");)

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

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.)

怪我太投入 2024-12-18 01:20:51

定义此方法:

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

然后将宏定义为该方法。

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.

在巴黎塔顶看东京樱花 2024-12-18 01:20:51

对于运行时检查,请使用如下所示的内容:

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

如果您在 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];

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:

@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];
温柔戏命师 2024-12-18 01:20:51
#define isIOS7 ([[[UIDevice currentDevice]systemVersion]floatValue] > 6.9) ?1 :0
#define isIOS7 ([[[UIDevice currentDevice]systemVersion]floatValue] > 6.9) ?1 :0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文