Conditional compiling of Objective-C in a Swift project

cocoa, ios, objective-c, swift

Solution

Add `#import "TargetConditionals.h"` in your source file.

#import "TargetConditionals.h"
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#else
#import <Cocoa/Cocoa.h>
#endif

Or add "-DTARGET_OS_IPHONE" to the "Other C Flags" section of the target build options.

Problem

I have an Objective-C category that I've been using for a while for both iOS and OSX projects and I want to use it in a Swift project as-is...until I have the time to translate it to Swift. Here's the top of my category's .h file: ``` #if TARGET_OS_IPHONE #import <UIKit/UIKit.h> #else #import <Cocoa/Cocoa.h> #endif ``` I've included it in the bridging header, but when I build the project for iOS, I get an error saying that it can't find the file Cocoa/Cocoa.h. Why is it even looking for it? Doesn't the conditional compile still work even in a Swift project? It's still compiling an Objective-C file. Thanks.

Original source