本文介绍了Xcode可以告诉我是否忘记在目标中包含类别实现吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个要导入.m的类别:
I have a category that I import in a .m thusly:
#import "UIView+Additions.h"
如果我忘记将 UIView + Additions.m
添加到目标中,那么直到运行时找不到运行选择器时,我才知道.有没有一种方法可以在编译时(或者可能在链接时)找出我忘记包含类别的实现的方法?
If I forget to add UIView+Additions.m
to my target, I won't know until runtime when the runtime can't find my selector. Is there a way to find out at compile time (or probably link time) that I forgot to include a category's implemtation?
推荐答案
此宏有效!
#ifndef HJBObjCCategory_h
#define HJBObjCCategory_h
#define HJBObjCCategoryInterface( c , n ) \
\
extern int c##n##Canary; \
\
__attribute__((constructor)) static void c##n##CanaryCage() { \
c##n##Canary = 0; \
} \
\
@interface c (n)
#define HJBObjCCategoryImplementation( c , n ) \
\
int c##n##Canary = 0; \
\
@implementation c ( n )
#endif
像这样使用它:
UIView + Additions.h
HJBObjCCategoryInterface(UIView, Additions)
- (void)hjb_foo;
@end
UIView + Additions.m
HJBObjCCategoryImplementation(UIView, Additions)
- (void)hjb_foo {
NSLog(@"foo!");
}
@end
这篇关于Xcode可以告诉我是否忘记在目标中包含类别实现吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!