Can Xcode tell me if I forget to include a category implementation in my target?

北慕城南 提交于 2019-12-24 18:19:10

问题


I have a category that I import in a .m thusly:

#import "UIView+Additions.h"

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?


回答1:


This macro works!

#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

Use it like this:

UIView+Additions.h

HJBObjCCategoryInterface(UIView, Additions)

- (void)hjb_foo;

@end

UIView+Additions.m

HJBObjCCategoryImplementation(UIView, Additions)

- (void)hjb_foo {
  NSLog(@"foo!");
}

@end


来源:https://stackoverflow.com/questions/17057352/can-xcode-tell-me-if-i-forget-to-include-a-category-implementation-in-my-target

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!