Xcode预处理器宏来检查Base SDK> = iOS 7.0 [英] Xcode preprocessor macro to check if Base SDK >= iOS 7.0

查看:193
本文介绍了Xcode预处理器宏来检查Base SDK> = iOS 7.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有在Base SDK为7.0或更高版本时,是否有任何预处理器宏来编译代码的某些部分? __IPHONE_7_0定义的常量似乎链接到iOS开发目标(而不是基本SDK)。

Is there any preprocessor macro to compile certain parts of code only if the Base SDK is 7.0 or higher? The "__IPHONE_7_0" defined constant seems to be linked to the iOS development target (and not to the base SDK).

我在iOS 7和iOS 6.1上使用XCode 5已经安装了。

I am using XCode 5 with iOS 7 and iOS 6.1 installed.

我之所以这么说是因为我正在将应用程序从iOS 6转换到iOS 7.我需要调整很多东西,而且我目前仍然希望用iOS 6.1作为基础SDK(以及开发目标iOS 6.0)编译我的应用程序,但是我想在每次使用iOS 7 SDK编译时添加一些我想要的代码,但是不能编译如果基础SDK是iOS 6.1。

The reason why I am asking this is that I am currently transitioning an app from iOS 6 to iOS 7. There are quite a few things to adjust, and I would currently still like to compile my app with the iOS 6.1 as base SDK (and with development target iOS 6.0), but would already like to add some code that I will want for whenever I compile with iOS 7 SDK, but which does not compile if base SDK is iOS 6.1.

示例:

if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
    [_tableView setSeparatorInset:UIEdgeInsetsZero];
}

上面这段代码不能用iOS 6.1 base SDK编译,因为它抱怨没有为UITableView定义setSeparatorInset。因此,我想在预处理器指令中包含这段代码,有条件地在基础SDK上。

This above piece of code does not compile with iOS 6.1 base SDK, as it complains about setSeparatorInset not being defined for UITableView. Hence I would like to include this piece of code inside a preprocessor directive, conditionally on the base SDK.

推荐答案

你应该阅读Apple的 SDK兼容性指南,其中解释了所有这些技术。

You should read Apple's SDK Compatibility Guide where all those techniques are explained.

特别是,他们建议使用 __ IPHONE_OS_VERSION_MIN_REQUIRED 宏来测试部署目标您的项目(支持的最低版本),对于您的情况使用 __ IPHONE_OS_VERSION_MAX_ALLOWED 宏来测试用于编译的Base SDK

In particular, they recommend to use the __IPHONE_OS_VERSION_MIN_REQUIRED macro to test against the Deployment Target of your project (minimum supported version), and for your case use the __IPHONE_OS_VERSION_MAX_ALLOWED macro to test the Base SDK used for compilation.

示例:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
// Only COMPILE this if compiled against BaseSDK iOS7.0 or greater
if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
   // Even when compiled with BaseSDK 7, only EXECUTE that if the user uses an
   // OS that support this method (namely if the user is running iOS7 or later,
   // but not for users running iOS6).
   [_tableView setSeparatorInset:UIEdgeInsetsZero];
}
#endif

重要提示:您应该使用数字比较中的常量,就好像你测试 #if __IPHONE_OS_VERSION_MAX_ALLOWED< __IPHONE_7_0 例如它在使用SDK 6时不起作用,因为 __ IPHONE_7_0 未定义,因此在该上下文中评估为0并且您的条件获胜按预期工作。

Important note: You should use numeric constants in your comparison as if you test #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_7_0 for example it won't work when using SDK 6, as __IPHONE_7_0 is not defined thus evaluated to 0 in that context and your condition won't work as expected.

这篇关于Xcode预处理器宏来检查Base SDK&gt; = iOS 7.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆