我可以为iOS和OSX使用一个类类别扩展名.h和.m文件吗?

时间:2014-01-22 01:15:21

标签: ios objective-c macos objective-c-category

是否有办法为OSX类别和iOS类别提供一个标题和实现文件?我不想让方法完全相同,并添加另外两个文件。

我试过这个,这不起作用:

#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)

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:6)

绝对!请记住,#define在编译之前是一个简单的文本替换:您希望kClassUIColor,而不是[UIColor class],就像您永远不会写@interface [UIColor class] (MyCategory)一样。

总结:

#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)
相关问题