基于oc的ios开发,如何给一个系统类添加一个属性? 目前我知道可以用继承或类别实现。但不知道哪种方式好
3条回答 默认 最新
- tongwei1987 2016-03-08 12:03关注
用类别:
.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
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 -
悬赏问题
- ¥20 access中怎么分割分别获取一下图中的值
- ¥15 keras_tcn已经安装成功,还是显示ModuleNotFoundError: No module named 'keras_tcn'
- ¥15 类图中关联与聚合的区别
- ¥15 ENVI高分五号去除云层的方法
- ¥15 16进制数据如何得到奇偶校验位
- ¥15 求合并两个字节流VB6代码
- ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
- ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
- ¥15 WPS访问权限不足怎么解决
- ¥15 java幂等控制问题