Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TARGET_OS_IPHONE and ApplicationTests

Tags:

Why doesn't this code work when compiling an ApplicationTests unit test bundle?

#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?

like image 392
Matt Baker Avatar asked Sep 18 '10 16:09

Matt Baker


2 Answers

You need to add

#include <TargetConditionals.h> 

source: https://opensource.apple.com/source/CarbonHeaders/CarbonHeaders-8A428/TargetConditionals.h.auto.html

like image 164
antho Avatar answered Jan 16 '23 10:01

antho


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

like image 31
fjoachim Avatar answered Jan 16 '23 10:01

fjoachim