您的位置:首页 > 移动开发 > IOS开发

iOS 如何给类别添加属性

2017-11-15 09:28 190 查看
.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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: