从编译器有条件隐藏代码 [英] Conditionally Hide Code from the Compiler

查看:207
本文介绍了从编译器有条件隐藏代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这里的问题。我设置为iOS发布更新,将解决一些问题在iOS 7.为了做到这一点,我需要使用一些特定的iOS 7功能/类型。我绝对确定iOS 7代码只会在iOS 7上执行,并回退到不同的代码为iOS 7之前。当然,我不允许提交与当前的Xcode测试版,所以我试图编译与当前Xcode版本。但是,我似乎找不到一种方法禁用此特殊警告:

So here's the problem. I'm set to release an update soon for iOS that will address some problems in iOS 7. In order to do this, I need to use some specific iOS 7 functions/types. I've made absolutely certain that iOS 7 code will only be executed on iOS 7 and fallback to different code for pre iOS 7. Of course, I'm not allowed to submit with the current Xcode beta, so I'm trying to compile with the current Xcode release. However, I can't seem to find a way to disable this particular warning:

使用未声明的标识符'<已编辑的> / code>

Use of undeclared identifier '<Redacted>'.

有没有人知道使用 #pragma 禁用此警告的方法。我尝试了一堆不同的,包括

Does anyone know of a way to disable this warning using a #pragma. I've tried a bunch of different ones including

-w / code>, -Wall

但没有任何效果。

UPDATE
答案:当然不能,因为编译器不能编译一个标识符。我的解决方案是简单地创建一个 #define

#define& 1

#define <redacted> 1

UPDATE 2
下面的答案使得它变得更加容易。我已经创建了一个 #define Xcode5Code(code,alt)允许我有条件地执行代码块。使用@maddy的解决方案修改它:

UPDATE 2 The answer below actually made it much easier. I had already created a #define Xcode5Code(code, alt) that allowed me to execute code blocks conditionally. By modifying it using the solution by @maddy:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
    #define Xcode5Code(code, alt) code
#else
    #define Xcode5Code(code, alt) alt
#endif

这使我可以通过使用以下代码轻松地从编译器中隐藏代码块:

This allows me to to easily hide blocks of code from the compiler by using:

Xcode5Code({
    //Code to be execute only with Xcode 5
}, {
    //code to be executed in previous versions of Xcode
})

使用 #define Xcode5Code 的主要好处是Xcode会自动完成它,这比使用完整的 #if __IPHONE_OS_VERSION_MAX_ALLOWED> = 70000 更容易,Xcode不会自动完成。

The main benefit of using the #define Xcode5Code is that Xcode will auto-complete it for you, which is a lot easier than using the full #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000, which Xcode won't auto-complete.

这实际上不会区分iOS 7和iOS 7之前的设备。它只区分当前Xcode可以处理的iOS版本。要区分我使用的iOS设备版本:

This won't actually distinguish between iOS 7 and pre iOS 7 devices. It only distinguishes what version of iOS the current Xcode can handle. To distinguish between iOS devices versions I use:

NSUInteger DeviceSystemMajorVersion(void) {
    static NSUInteger _deviceSystemMajorVersion = -1;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _deviceSystemMajorVersion = [[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] objectAtIndex:0] intValue];
    });
    return _deviceSystemMajorVersion;
}

上面是苹果的代码。为了围绕NDA跳舞一点,我会说这有助于布局根控制器的视图,因为这取决于您使用的Xcode版本和设备上的iOS版本。如果你试图管理测试版和生产代码,这可以帮助很多。一旦您可以使用Xcode 5提交应用程序, #define Xcode5Code 将不再需要。

The above is Apple's code, by the way. To dance around the NDA a little, I'll say that this helps with laying out a root controller's view, because that depends on both the version of Xcode you're using AND the version of iOS that's on the device. And if you're trying to manage beta's as well as production code, this can help a lot. Once you can submit apps with Xcode 5, the #define Xcode5Code will no longer be necessary.

推荐答案

如果您想使用两个不同版本的Xcode或两个不同的Base SDK设置编译您的应用程序,那么您应该使用编译器指令:

If you want to compile your app with two difference versions of Xcode or two different Base SDK settings then you should use compiler directives:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000 // iOS 7.0 supported
    // iOS 7 code here
#else
    // Pre-iOS 7 code here
#endif

不要将此用于运行时检查。此解决方案仅在必须使用两个不同版本编译代码时使用。一个例子是你添加了iOS 7代码,但是你仍然需要使用Xcode 4.6编译代码。使用compile指令可以使用旧的Base SDK从编译器隐藏iOS 7代码。

Do not use this for runtime checks. This solution is only to be used when you must compile your code with two different versions. An example would be you have added iOS 7 code but you still need to compile the code with Xcode 4.6. Using the compile directives allows you to "hide" the iOS 7 code from the compiler using the older Base SDK.

有关详细信息,请参阅文档中的SDK兼容性指南对此和正确的运行时检查。

See the "SDK Compatibility Guide" in the docs for more on this and proper runtime checks.

这篇关于从编译器有条件隐藏代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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