Objective-C語言在Category中實現屬性

weixin_34146805發表於2016-02-06

做開發時我們常常會需要在已經實現了的類中增加一些方法,這時候我們一般會用Category的方式來做。但是這樣做我們也只能擴充套件一些方法,而有時候我們更多的是想給它增加一個屬性。由於類已經是編譯好的了,就不能靜態的增加成員了,這樣我們就需要自己來實現getter和setter方法了,在這些方法中動態的讀寫屬性變數來實現屬性。一種比較簡單的做法是使用Objective-C執行時的這兩個方法:

void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy);
id objc_getAssociatedObject(id object, const void *key);

這兩個方法可以讓一個物件和另一個物件關聯,就是說一個物件可以保持對另一個物件的引用,並獲取那個物件。有了這些,就能實現屬性功能了。 policy可以設定為以下這些值:

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的例子:

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屬性(如果需要其他型別的屬性可自行修改)。

#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屬性:

@interface UIAlertView (Ex)
@property(nonatomic, retain) id userInfo;
@property(nonatomic) CGRect customArea;
@end

@implementation UIAlertView (Ex)
SYNTHESIZE_CATEGORY_OBJ_PROPERTY(userInfo, setUserInfo:)SYNTHESIZE_CATEGORY_VALUE_PROPERTY(CGRect, customArea, setCustomArea:)
@end

相關文章