美文网首页iOS
给类别添加属性

给类别添加属性

作者: 代码侯 | 来源:发表于2016-09-17 23:28 被阅读0次

.h

#import <Foundation/Foundation.h>

@interface NSObject (MyCatgory)

@property (nonatomic, strong) NSString *propertyName;

@end

.m

#import "NSObject+PropertyName.h"

#import <objc/runtime.h>

static const void *PropertyName = &PropertyName;

@implementation NSObject (navigationBar)

@dynamic propertyName;

- (NSString *)propertyName {

    return objc_getAssociatedObject(self, PropertyName);
}

- (void)setPropertyName:(NSString *)propertyName {

    objc_setAssociatedObject(self, PropertyName, propertyName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

使用

NSObject *obj = [[NSObject alloc] init];
obj.propertyName = @"11";
NSLog(@"%@",obj.propertyName);

相关文章

网友评论

    本文标题:给类别添加属性

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