iOS 如何给类别添加属性

.H 文件

#import <UIKit/UIKit.h>

@interface UIViewController (statistics)

@property (nonatomic, strong, readwrite) id myCustomProperty;

@end

.M 文件

static void * myCustomCategoryPorpertyKey = (void *)@"myCustomCategoryPorpertyKey";

- (id)myCustomProperty {
    return objc_getAssociatedObject(self, myCustomCategoryPorpertyKey);
}

- (void)setMyCustomProperty:(id)myCustomProperty {
    objc_setAssociatedObject(self, myCustomCategoryPorpertyKey, myCustomProperty, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


猜你喜欢

转载自blog.csdn.net/qq_28379951/article/details/78537290