iOS - 分类添加属性之关联引用

分类是不能合成属性的,因为合成属性会生成对应的实例变量,而分类是不允许添加实例变量的(实例变量所在内存区域已初始化为不可更改,无法在动态运行时修改之)。

虽然不能增加实例变量,但是添加属性还是可以的,只不过需要自己在分类中实现get和set方法,同时标记属性为动态获取。

其中一种方法叫做关联引用,实现了用存取器来访问属性,当然实际上并没有实例变量,所以本质上只不过是增加了两个方法而已。

首先在.h中声明属性:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface UIViewController (PropertyTest)  
  4. @property (nonatomiccopyNSString *testProperty;  
  5. @end  
然后在.m中实现:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #import "UIViewController+PropertyTest.h"  
  2. #import <objc/runtime.h>  
  3.   
  4. const char kTestProperty;//声明一个唯一地址的key。或用static char kTestProperty; 
  5.   
  6. @implementation UIViewController (PropertyTest)  
  7.   
  8. @dynamic testProperty;//运行时动态获取  
  9.   
  10. - (void)setTestProperty:(NSString *)testProperty{  
  11.     objc_setAssociatedObject(self, &kTestProperty, testProperty, OBJC_ASSOCIATION_COPY_NONATOMIC);  
  12. }  
  13.   
  14. - (NSString *)testProperty{  
  15.     return objc_getAssociatedObject(self, &kTestProperty);  
  16. }  
  17.   
  18. @end  
然后用代码测试一下:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. self.testProperty = @"testProperty";  
  2.     self.testProperty = [self.testProperty stringByAppendingString:@"add string"];  
  3.     NSLog(@"%@"self.testProperty);  
打印如下:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. 2015-05-17 15:12:36.816 Test_5_17[3508:1209580] testPropertyadd string  

可见实现了属性的get和set功能。


=====================================================================================================



给NSString类添加两种类型的属性, 字符串类型的tag值strFlag, 以及int型的tag值intTag. 

定义这个两个属性的set和get方法:

[objc]  view plain  copy
  1. <span style="font-size:18px;">@interface NSString (Ass)  
  2.   
  3. // 对象属性的set和get  
  4. - (void)setStrFlag:(NSString *)strFlag;  
  5. - (NSString *)strFlag;  
  6.   
  7. // 非对象属性的set和get  
  8. - (void)setIntFlag:(int)intFlag;  
  9. - (int)intFlag;  
  10.   
  11. @end</span>  
实现这四个方法:

需导入runtime头文件:

[objc]  view plain  copy
  1. #import <objc/runtime.h>  
[objc]  view plain  copy
  1. @implementation NSString (Ass)  
  2.   
  3. static int _intFlag;  
  4.   
  5. static NSString *_strFlag;  
  6.   
  7. - (void)setStrFlag:(NSString *)flag {  
  8.     // void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)  
  9.     objc_setAssociatedObject(self, &_strFlag, flag, OBJC_ASSOCIATION_RETAIN_NONATOMIC);  
  10. }  
  11.   
  12. - (NSString *)strFlag {  
  13.     // id objc_getAssociatedObject(id object, const void *key)  
  14.     return objc_getAssociatedObject(self, &_strFlag);  
  15. }  
  16.   
  17. - (void)setIntFlag:(int)intFlag {  
  18.     NSNumber *t = @(intFlag);  
  19.     // void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)  
  20.     objc_setAssociatedObject(self, &_intFlag, t, OBJC_ASSOCIATION_RETAIN_NONATOMIC);  
  21. }  
  22. - (int)intFlag {  
  23.     // id objc_getAssociatedObject(id object, const void *key)  
  24.     NSNumber *t = objc_getAssociatedObject(self, &_intFlag);  
  25.     return (int)[t integerValue];  
  26. }  
  27.   
  28. @end  
使用规则和类别差不多, 一个是拓展了方法, 一个是拓展了属性而已:

[objc]  view plain  copy
  1. NSString *str = @"Macro "// 定义一个系统的NSString对象  
  2. str.strFlag = @"abc"// 给字符串tag赋值  
  3. str.intFlag = 456// 给int型tag赋值  
  4. NSLog(@"%@:%@-%d", str, str.strFlag, str.intFlag); // 使用拓展的属性  

工程源码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值