美文网首页
类别添加属性

类别添加属性

作者: 雾霭天涯 | 来源:发表于2019-03-14 19:08 被阅读0次
    #import <Foundation/Foundation.h>
    @interface NSObject (Common)
    @property (nonatomic,copy) NSString* name;
    @property (nonatomic,assign) NSInteger aID;
    @end
    
    #import "NSObject+Common.h"
    #import <objc/runtime.h>
    static NSString* nameKey = @"nameKey";
    static NSString* aIDKey = @"aIDKey";
    
    @implementation NSObject (Common)
    
    - (void)setName:(NSString*)name{
        objc_setAssociatedObject(self,&nameKey, name, OBJC_ASSOCIATION_COPY);
    }
    - (NSString*)name{
        return objc_getAssociatedObject(self, &nameKey);
    }
    
    - (void)setAID:(NSInteger)aID{
        objc_setAssociatedObject(self, &aIDKey, [NSString stringWithFormat:@"%ld",(long)aID], OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    - (NSInteger)aID{
        return [objc_getAssociatedObject(self, &aIDKey) integerValue];
    }
    @end
    
    

    相关文章

      网友评论

          本文标题:类别添加属性

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