xcode TARGET_OS_IPHONE 和 ApplicationTests
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3742525/
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
TARGET_OS_IPHONE and ApplicationTests
提问by Matt Baker
Why doesn't this code work when compiling an ApplicationTests unit test bundle?
为什么在编译 ApplicationTests 单元测试包时这段代码不起作用?
#if TARGET_OS_IPHONE
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#else
#import <Cocoa/Cocoa.h>
#endif
One of my dependencies has this check and compiles just fine in my main application bundles, but it tries to load <Cocoa/Cocoa.h>
when compiling my ApplicationTests bundle. It's probably just my lack of understanding of Xcode, but I get nervous when my test bundles don't build. Any suggestions?
我的一个依赖项有这个检查并且在我的主应用程序包中编译得很好,但是它<Cocoa/Cocoa.h>
在编译我的 ApplicationTests 包时尝试加载。这可能只是我对 Xcode 缺乏了解,但是当我的测试包没有构建时我会感到紧张。有什么建议?
采纳答案by James J
I had a similar problem: TARGET_OS_IPHONE
isn't defined when building a static library. My solution was to add "-DTARGET_OS_IPHONE
" to the "Other C Flags
" section of the target build options.
我有一个类似的问题:TARGET_OS_IPHONE
在构建静态库时未定义。我的解决方案是将“ -DTARGET_OS_IPHONE
”添加到Other C Flags
目标构建选项的“ ”部分。
回答by antho
You need to add
你需要添加
#import "TargetConditionals.h"
source: https://opensource.apple.com/source/CarbonHeaders/CarbonHeaders-8A428/TargetConditionals.h.auto.html
来源:https: //opensource.apple.com/source/CarbonHeaders/CarbonHeaders-8A428/TargetConditionals.h.auto.html
回答by fjoachim
The simplest solution is to move the #import <Foundation/Foundation.h>
statement out if the #if
condition and replace Cocoa with AppKit like this:
最简单的解决方案是将#import <Foundation/Foundation.h>
语句移出 if#if
条件并用 AppKit 替换 Cocoa,如下所示:
#import <Foundation/Foundation.h>
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#else
#import <AppKit/AppKit.h>
#endif
The Foundation umbrella header imports the NSObjCRuntime header which in turn imports the TargetConditionals header.
Foundation 伞形标头导入 NSObjCRuntime 标头,而 NSObjCRuntime 标头又导入 TargetConditionals 标头。
回答by Zsolt Szatmari
It imposes no performance penalty, although it can hurt compile times. That said, it isn't really an issue for Objective C. However, it can really hurt when dealing with C++ classes.
它不会造成性能损失,尽管它会影响编译时间。也就是说,这对于 Objective C 来说并不是真正的问题。但是,在处理 C++ 类时它确实会受到伤害。
回答by Bill
Both work well
两者都运行良好
#import "TargetConditionals.h"
#import <Foundation/Foundation.h>
#import "TargetConditionals.h"
#import <Foundation/Foundation.h>
<Foundation/Foundation.h>
|
└-#import <Foundation/NSObjCRuntime.h>
|
└- #include <TargetConditionals.h>
|
└- defined TARGET_OS_IPHONE