Can preprocessor directives be used to import different header files for Mac and iOS?

I am writing a class library for Mac OS X and iOS that will be released as Cocoa Framework for OS X and a static library for iOS. To simplify things, I intend to use several goals in Xcode. However, classes on Mac OS X refer to Cocoa.h, while on iOS they refer to Foundation.h.

My questions are basically these:

  • Can Mac OS X framework replace Background.framework? The classes used in the framework are NSString, NSMutableString, and NSMutableArray.
  • Or I could use preprocessor directives in header files to control the inclusion of a structure, for example.

    #ifdef MacOSX #import <Cocoa/Cocoa.h> #else #import <Foundation/Foundation.h> #endif 
+8
c-preprocessor objective-c
source share
4 answers

You can use them to separate platform-specific code (see TargetConditionals.h ):

 #ifdef TARGET_OS_IPHONE // iOS #elif defined TARGET_IPHONE_SIMULATOR // iOS Simulator #elif defined TARGET_OS_MAC // Other kinds of Mac OS #else // Unsupported platform #endif 

Here is a useful diagram .

+20
source share

It works like a charm:

 #ifdef __APPLE__ #include "TargetConditionals.h" #if TARGET_IPHONE_SIMULATOR // ios simulator #elif TARGET_OS_IPHONE // ios device #elif TARGET_OS_MAC // mac os #else // Unsupported platform #endif #endif 
+8
source share
  • Can Mac OS X framework replace Background.framework? The classes used in the framework are NSString, NSMutableString, and NSMutableArray.

Try and see. If compilation failed, no. If it succeeds, yes.

  • Or I can use preprocessor directives in header files to control the inclusion of a structure, for example.

Yes, you can. In fact, I think this is the only way to do this.

+2
source share

This works fine for me:

 #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED //iOS #else //Mac #endif 
+1
source share

All Articles