基於oc的ios開發,如何給一個系統類添加一個屬性? 目前我知道可以用繼承或類別實現。但不知道哪種方式好
用類別:
.h文件
#import
@interface UIButton (TLButton)
@property(nonatomic,strong,readwrite)NSNumber* cityId;
@property(nonatomic,strong,readwrite)NSNumber* typeId;
@property(nonatomic,readwrite)NSNumber* pross;
@end
.m文件
#import "UIButton+TLButton.h"
@implementation UIButton (TLButton)
-(NSNumber *)cityId
{
return objc_getAssociatedObject(self, @selector(cityId));
}
(void)setCityId:(NSNumber *)cityId
{
[self willChangeValueForKey:@"cityId"];
objc_setAssociatedObject(self, @selector(cityId), cityId, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self didChangeValueForKey:@"cityId"];
}
-(NSNumber )typeId
{
return objc_getAssociatedObject(self, @selector(typeId));
}
-(void)setTypeId:(NSNumber *)typeId
{
[self willChangeValueForKey:@"typeId"];
objc_setAssociatedObject(self, @selector(typeId), typeId, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self didChangeValueForKey:@"typeId"];
}
-(NSNumber)pross
{
return objc_getAssociatedObject(self, @selector(pross));
}
-(void)setPross:(NSNumber*)pross
{
[self willChangeValueForKey:@"pross"];
objc_setAssociatedObject(self, @selector(pross), pross, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self didChangeValueForKey:@"pross"];
}
@end