Category中添加属性

2020-03-02  本文已影响0人  ADreamClusive
#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
上一篇下一篇

猜你喜欢

热点阅读