美文网首页
Category中添加属性

Category中添加属性

作者: ADreamClusive | 来源:发表于2020-03-02 18:42 被阅读0次
    #import <Foundation/Foundation.h>
    @interface SmClass (Additional)
    @property (nonatomic, strong) NSMutableArray *someContainer;
    @property (nonatomic, strong) id anotherObj;
    @end
    
    #import "SmClass+Additional.h"
    #import <objc/runtime.h>
    const void *containerKey = &containerKey;
    @implementation SmClass (additional)
    - (NSMutableArray *)someContainer
    {
        return objc_getAssociatedObject(self, &containerKey);
    }
    - (void)setSomeContainer:(NSMutableArray *)someContainer
    {
        objc_setAssociatedObject(self, &containerKey, someContainer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    // 
    - (id)anotherObj {
        return objc_getAssociatedObject(self, _cmd);
    }
    - (void)setAnotherObj:(id)obj {
        objc_setAssociatedObject(self, @selector(anotherObj), obj, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    @end
    

    相关文章

      网友评论

          本文标题:Category中添加属性

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