用于检查 Base SDK >= iOS 7.0 的 Xcode 预处理器宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19789958/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:45:20  来源:igfitidea点击:

Xcode preprocessor macro to check if Base SDK >= iOS 7.0

iosxcodeios7

提问by quentinadam

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

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

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

我在安装了 iOS 7 和 iOS 6.1 的情况下使用 XCode 5。

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.

我问这个的原因是我目前正在将应用程序从 iOS 6 转换到 iOS 7。有很多需要调整的地方,我目前仍然希望使用 iOS 6.1 作为基础 SDK(和与开发目标 iOS 6.0),但已经想添加一些代码,每当我使用 iOS 7 SDK 进行编译时,我都会想要这些代码,但如果基本 SDK 是 iOS 6.1,则不会编译。

Example:

例子:

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

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.

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

回答by AliSoftware

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

您应该阅读 Apple 的SDK Compatibility Guide,其中解释了所有这些技术。

In particular, they recommend to use the __IPHONE_OS_VERSION_MIN_REQUIREDmacro to test against the Deployment Targetof your project (minimum supported version), and for your case use the __IPHONE_OS_VERSION_MAX_ALLOWEDmacro to test the Base SDK used for compilation.

特别是,他们建议使用__IPHONE_OS_VERSION_MIN_REQUIRED宏来测试项目的部署目标(支持的最低版本),并针对您的情况使用__IPHONE_OS_VERSION_MAX_ALLOWED宏来测试用于编译的基础 SDK



Example:

例子:

#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

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

重要说明:您应该在比较中使用数字常量,就好像您#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_7_0在使用 SDK 6 时测试它不会工作一样,因为__IPHONE_7_0没有定义,因此在该上下文中评估为 0,并且您的条件不会按预期工作。

回答by rckoenes

Yes you can use the __IPHONE_7_0define:

是的,您可以使用__IPHONE_7_0定义:

#ifdef __IPHONE_7_0
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView setSeparatorInset:UIEdgeInsetsZero];
    }
#endif

回答by Mojtaba

As per Apple Doc you should use NSFoundationVersionNumberto distinguish between iOS 7 and others. You can make it simpler using following macros:

根据 Apple Doc,您应该使用它NSFoundationVersionNumber来区分 iOS 7 和其他。您可以使用以下宏使其更简单:

#define isIOS6 floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1 
#define isIOS7 floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1

and then later in code do

然后在代码中做

#ifdef __IPHONE_7_0
    if (isIOS7) {
       // Do additional stuff for iOS 7.
    } 
#endif

Yes you should check on both compile-time (with #ifdef) and run-time (with isIOS7) this way you will be able to compile with iOS6 SDK , iOS7 SDK, and also iOS7 SDK with an iOS6 target.

是的,您应该检查编译时(with #ifdef)和运行时(with isIOS7),这样您就可以使用 iOS6 SDK、iOS7 SDK 以及带有 iOS6 目标的 iOS7 SDK 进行编译。

OH! Remember that you cannot do if (!isIOS7)you have to use if (isIOS6).

哦!请记住,您不能这样做,if (!isIOS7)您必须使用if (isIOS6).

https://developer.apple.com/library/ios/documentation/userexperience/conceptual/transitionguide/

https://developer.apple.com/library/ios/documentation/userexperience/conceptual/transitionguide/

回答by Akki

Also, you can use this macro

此外,您可以使用此宏

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:(v) options:NSNumericSearch] != NSOrderedAscending)

e.g if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.1")) {[self doSomething];} else {[self doSomethingElse];}

例如 if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.1")) {[self doSomething];} else {[self doSomethingElse];}

回答by Daij-Djan

You shouldn't test for the sdk but for availability of the method / class IMHO. So not with a precompiled at all

您不应该测试 sdk,而是测试方法/类的可用性恕我直言。所以根本没有预编译