Conditional Compilation of Objective-C in a Swift Project

I have an Objective-C category that I have used for some time for iOS and OSX projects, and I want to use them in my Swift project as is ... until I have time to switch it to Swift.

At the top of my category is an .h file:

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

I included it in the header of the bridge, but when I create a project for iOS, I get an error that it cannot find the Cocoa / Cocoa.h file.

Why is he even looking for him? Does conditional compilation not work even in a Swift project? It still compiles the Objective-C file.

Thank.

+4
source share
1 answer

Add #import "TargetConditionals.h"to the source file.

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

"-DTARGET_OS_IPHONE" " C" .

+11

Source: https://habr.com/ru/post/1547836/


All Articles