做开发时我们常常会需要在已经实现了的类中增加一些方法,这时候我们一般会用Category的方式来做。但是这样做我们也只能扩展一些方法,而有时候我们更多的是想给它增加一个属性。由于类已经是编译好的了,就不能静态的增加成员了,这样我们就需要自己来实现getter和setter方法了,在这些方法中动态的读写属性变量来实现属性。一种比较简单的做法是使用Objective-C运行时的这两个方法:
1 2
| void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy); id objc_getAssociatedObject(id object, const void *key);
|
这两个方法可以让一个对象和另一个对象关联,就是说一个对象可以保持对另一个对象的引用,并获取那个对象。有了这些,就能实现属性功能了。
policy可以设置为以下这些值:
1 2 3 4 5 6 7
| enum { OBJC_ASSOCIATION_ASSIGN = 0, OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, OBJC_ASSOCIATION_COPY_NONATOMIC = 3, OBJC_ASSOCIATION_RETAIN = 01401, OBJC_ASSOCIATION_COPY = 01403 };
|
这些值跟属性定义中的nonatomic,copy,retain等关键字的功能类似。
Example
下面是一个属性自定义getter和setter的例子:
1 2 3 4 5 6 7 8 9
| NSString const * kExposeController = @"exposeController"; - (UIViewController *)exposeController { return (UIViewController *)objc_getAssociatedObject(self, kExposeController); } - (void)setExposeController:(UIViewController *)exposeController { objc_setAssociatedObject(self, kExposeController, exposeController, OBJC_ASSOCIATION_RETAIN_NONATOMIC); }
|
可以看出使用objc_setAssociatedObject和objc_getAssociatedObject函数可以很方便的实现属性的getter和setter。
一个很方便的宏
为此,我特意写了一个Synthesize宏,可以提供@synthesize类似的功能。可以支持两种最常用的属性:非原子retain和assign属性(如果需要其他类型的属性可自行修改)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #import <objc/runtime.h> #define SYNTHESIZE_CATEGORY_OBJ_PROPERTY(propertyGetter, propertySetter) - (id) propertyGetter { return objc_getAssociatedObject(self, @selector( propertyGetter )); } - (void) propertySetter (id)obj{ objc_setAssociatedObject(self, @selector( propertyGetter ), obj, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } #define SYNTHESIZE_CATEGORY_VALUE_PROPERTY(valueType, propertyGetter, propertySetter) - (valueType) propertyGetter { valueType ret = {0}; [objc_getAssociatedObject(self, @selector( propertyGetter )) getValue:&ret]; return ret; } - (void) propertySetter (valueType)value{ NSValue *valueObj = [NSValue valueWithBytes:&value objCType:@encode(valueType)]; objc_setAssociatedObject(self, @selector( propertyGetter ), valueObj, OBJC_ASSOCIATION_RETAIN_NONATOMIC); }
|
用这个宏只需要指定相关属性的类型,getter和setter就可以快速的实现一个属性。比如在UIAlert的Category实现一个非原子retain属性userInfo,以及一个assign的类型为CGRect的customArea属性:
UIAlertView+Ex.h
1 2 3 4
| @interface UIAlertView (Ex) @property(nonatomic, retain) id userInfo; @property(nonatomic) CGRect customArea; @end
|
UIAlertView+Ex.m
```
@implementation UIAlertView (Ex)
SYNTHESIZE_CATEGORY_OBJ_PROPERTY(userInfo, setUserInfo:)
SYNTHESIZE_CATEGORY_VALUE_PROPERTY(CGRect, customArea, setCustomArea:)
@end