Can I use the .h and .m class extension for iOS and OSX?

Is there a way to have one header and implementation file for both OSX categories and iOS categories? I do not want the methods to be the same and add two more files.

I tried this that does not work:

#if TARGET_OS_IPHONE
#define kClass [UIColor class]
#import <UIKit/UIKit.h>
#elif TARGET_OS_MAC
#define kClass [NSColor class]
#import <AppKit/AppKit.h>
#endif

@interface kClass (MyCategory)

Is there any way to do this?

+4
source share
1 answer

Absolutely! Just remember that it #defineis a simple substitute for text before compilation: you want it to kClassbe UIColor, and not [UIColor class], just like you will never write @interface [UIColor class] (MyCategory).

Finally:

#if TARGET_OS_IPHONE
#define kClass UIColor
#import <UIKit/UIKit.h>
#elif TARGET_OS_MAC
#define kClass NSColor
#import <AppKit/AppKit.h>
#endif

@interface kClass (MyCategory)
+6
source

All Articles