美文网首页
__attribute__

__attribute__

作者: iVikings | 来源:发表于2018-11-21 11:07 被阅读19次
    • objc_subclassing_restricted

    定义一个 不可被继承的类,如下代码,在 @interface WBTools : NSObject 前面加上 __attribute__((objc_subclassing_restricted)) 即可

    __attribute__((objc_subclassing_restricted))
    @interface WBTools : NSObject
    @end
    
    @implementation WBTools
    @end
    
    /** ❌ Cannot subclass a class that was declared with the 'objc_subclassing_restricted' attribute */
    @interface WBNetworkTools : WBTools
    @end
    
    @implementation WBNetworkTools
    @end
    
    • objc_requires_super

    NS_REQUIRES_SUPER 标识子类继承父类方法时,需要调用 [super xxMethod] 方法,否则给出编译警告 ⚠️

    @interface WBTools : NSObject
    /** 写法一 */
    - (void)initialize __attribute__((objc_requires_super));
    /** 写法二 */
    - (void)wb_initialize NS_REQUIRES_SUPER;
    @end
    
    @implementation WBTools
    - (void)initialize { }
    - (void)wb_initialize { }
    @end
    
    @interface WBNetworkTools : WBTools
    
    @end
    
    @implementation WBNetworkTools
    - (void)initialize {
        /** ⚠️ Method possibly missing a [super initialize] call */
    }
    - (void)wb_initialize {
        /** ⚠️ Method possibly missing a [super wb_initialize] call */
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:__attribute__

          本文链接:https://www.haomeiwen.com/subject/lgpqqqtx.html