给类别添加属性

.h

#import 

@interface NSObject (MyCatgory)

@property (nonatomic, strong) NSString *propertyName;

@end

.m

#import "NSObject+PropertyName.h"

#import 

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);

你可能感兴趣的:(给类别添加属性)