Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional compiling of Objective-C in a Swift project

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.

like image 244
pizzafilms Avatar asked Dec 11 '22 04:12

pizzafilms


1 Answers

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.

like image 182
Kazuki Sakamoto Avatar answered Feb 11 '23 22:02

Kazuki Sakamoto