【问题标题】:Can Xcode tell me if I forget to include a category implementation in my target?如果我忘记在目标中包含类别实现,Xcode 可以告诉我吗?
【发布时间】:2013-06-12 03:44:48
【问题描述】:

我有一个类别,因此我在 .m 中导入:

#import "UIView+Additions.h"

如果我忘记将UIView+Additions.m 添加到我的目标,直到运行时我才会知道运行时找不到我的选择器。有没有办法在编译时(或者可能是链接时)找出我忘记包含类别的实现?

【问题讨论】:

    标签: objective-c xcode linker objective-c-category


    【解决方案1】:

    这个宏有效!

    #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
    

    【讨论】:

      猜你喜欢
      • 2011-04-09
      • 1970-01-01
      • 1970-01-01
      • 2012-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-14
      • 2013-02-05
      相关资源
      最近更新 更多