在Mac和iPhone特定代码之间切换时使用哪种条件编译? [英] Which conditional compile to use to switch between Mac and iPhone specific code?

查看:69
本文介绍了在Mac和iPhone特定代码之间切换时使用哪种条件编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个包含共享代码的Mac应用程序和iPad应用程序的项目中.如何使用条件编译开关从iPhone项目中排除Mac专用代码,反之亦然?我注意到TARGET_OS_IPHONETARGET_OS_MAC均为1,因此它们始终为true.我是否可以使用另一个开关,该开关仅在针对特定目标进行编译时才返回true?

I am working on a project that includes a Mac application and an iPad application that share code. How can I use conditional compile switches to exclude Mac-specific code from the iPhone project and vice-versa? I've noticed that TARGET_OS_IPHONE and TARGET_OS_MAC are both 1, and so they are both always true. Is there another switch I can use that will only return true when compiling for a specific target?

在大多数情况下,我已经通过将#include <UIKit/UIKit.h>#include <Cocoa/Cocoa.h>移入两个项目的预编译头中来进行协作.我正在共享模型和一些实用程序代码,这些代码可从RSS feed和Evernote获取数据.

For the most part, I've gotten the files to cooperate by moving #include <UIKit/UIKit.h> and #include <Cocoa/Cocoa.h> into the precompile headers for the two projects. I'm sharing models and some utility code that fetches data from RSS feeds and Evernote.

尤其是,对于选项参数iOS 3.2和更早版本以及Mac OS 10.5和更早版本,[NSData dataWithContentsOfURL:options:error:]函数采用的常数与iOS 4和Mac OS 10.6所采用的常数不同.我使用的条件是:

In particular, the [NSData dataWithContentsOfURL:options:error:] function takes a different constant for the options parameter iOS 3.2 and earlier and Mac OS 10.5 and earlier than it does for iOS 4 and Mac OS 10.6. The conditional I'm using is:

#if (TARGET_OS_IPHONE && (__IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_3_2)) || (TARGET_OS_MAC && (MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_5))

这似乎可行,但是我想确保这是防弹的.我的理解是,如果Mac版本设置为10.6,而iOS版本设置为3.2,则即使为iOS 3.2进行编译,它仍将使用新的常量,这似乎是不正确的.

This seems to work, but I want to make sure this is bulletproof. My understanding is that if the Mac version is set to 10.6, but the iOS version is set to 3.2, it will still use the new constants even if it's compiling for iOS 3.2, which seems incorrect.

在此先感谢您的帮助!

推荐答案

您在观察中犯了一个错误. :)

You've made a mistake in your observations. :)

TARGET_OS_MAC将为1.没错,对于这种事情,它是毫无用处的.

TARGET_OS_MAC will be 1 when building a Mac or iPhone application. You're right, it's quite useless for this sort of thing.

但是,在构建Mac应用程序时,TARGET_OS_IPHONE为0.为此,我一直在标题中使用TARGET_OS_IPHONE.

However, TARGET_OS_IPHONE is 0 when building a Mac application. I use TARGET_OS_IPHONE in my headers all the time for this purpose.

赞:

#if TARGET_OS_IPHONE
// iOS code
#else
// OSX code
#endif

这是一张很棒的图表: http://sealiesoftware.com/blog/archive/2010/8/16 /TargetConditionalsh.html

Here's a great chart on this: http://sealiesoftware.com/blog/archive/2010/8/16/TargetConditionalsh.html

这篇关于在Mac和iPhone特定代码之间切换时使用哪种条件编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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