标签:
想到要如何为所有的对象增加实例变量吗?我们知道,使用Category可以很方便地为现有的类增加方法,但却无法直接增加实例变量。不过从Mac OS X v10.6开始,系统提供了Associative References,这个问题就很容易解决了。这种方法也就是所谓的关联(association),我们可以在runtime期间动态地添加任意多的属性,并且随时读取。所用到的两个重要runtime API是:
OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);
现在我们结合一个实际的例子来说明他们的用法。假设我们现在打算利用category对UILabel进行属性补充,可以这么做:
#import <UIKit/UIKit.h> #import <objc/runtime.h> @interface UILabel (Associate) - (void) setFlashColor:(UIColor *) flashColor; - (UIColor *) getFlashColor; @end
#import "UILabel+Associate.h" @implementation UILabel (Associate) static char flashColorKey; - (void) setFlashColor:(UIColor *) flashColor{ objc_setAssociatedObject(self, &flashColorKey, flashColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (UIColor *) getFlashColor{ return objc_getAssociatedObject(self, &flashColorKey); } @end
上面的例子有几个需要注意的地方:
1、key:我们注意到在函数签名中key的类型const void *,这表示key仅仅是一个地址,而不是字符串的内容,这也是为说明flashColorKey没有初始化的原因,因为具体指向什么内容我们无所谓,我们要的仅仅是地址!如果在setAssocaitedObject中你传入的是flashColorKey,那get方法得到的值将会是nil。正确的应该是传入地址&flashColorKey。
2、policy:这里的policy跟属性声明中的retain、assign、copy是一样的,不再赘述
下面我们再来看另一个例子,来源于APPLE GUIDE:
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
int main (int argc, const char * argv[]) {
@autoreleasepool {
/*Seciton 0. 关联数据的Key和Value*/
static char overviewKey;
static const char *myOwnKey = "VideoProperty\0";
static const char intValueKey = ‘i‘;
NSArray *array = [[NSArray alloc]
initWithObjects:@ "One", @"Two", @"Three", nil];
// For the purposes of illustration, use initWithFormat: to ensure
// we get a deallocatable string
NSString *overview = [[NSString alloc]
initWithFormat:@"%@", @"First three numbers"];
NSString *videoKeyValue = @"This is a video";
NSNumber *intValue = [[NSNumber alloc]initWithInt:5];
/*Section 1. 关联数据设置部分*/
objc_setAssociatedObject (
array,
&overviewKey,
overview,
OBJC_ASSOCIATION_RETAIN
);
[overview release];
objc_setAssociatedObject (
array,
myOwnKey,
videoKeyValue,
OBJC_ASSOCIATION_RETAIN
);
objc_setAssociatedObject (
array,
&intValueKey,
intValue,
OBJC_ASSOCIATION_RETAIN
);
/*Section 3. 关联数据查询部分*/
NSString *associatedObject = (NSString *) objc_getAssociatedObject (array, &overviewKey);
NSLog(@"associatedObject: %@", associatedObject);
NSString *associatedObject2 = (NSString *) objc_getAssociatedObject(array, myOwnKey);
NSLog(@"Video Key value is %@", associatedObject2);
NSString *assObject3 = (NSString *) objc_getAssociatedObject(array, &myOwnKey);
if( assObject3 )
{
NSLog(@"不会进入这里! assObject3 应当为nil!");
}
else
{
NSLog(@"OK. 通过myOwnKey的地址是得不到数据的!");
}
NSNumber *assKeyValue = (NSNumber *) objc_getAssociatedObject(array, &intValueKey);
NSLog(@"Int value is %d",[assKeyValue intValue]);
/*Section 3. 关联数据清理部分*/
objc_setAssociatedObject (
array,
&overviewKey,
nil,
OBJC_ASSOCIATION_ASSIGN
);
objc_setAssociatedObject (
array,
myOwnKey,
nil,
OBJC_ASSOCIATION_ASSIGN
);
objc_setAssociatedObject (
array,
&intValueKey,
nil,
OBJC_ASSOCIATION_ASSIGN
);
[array release];
}
return 0;
}
【原】iOS中动态添加属性的方法——关联(e.g. 向Category添加成员变量)
标签:
原文地址:http://www.cnblogs.com/wengzilin/p/4331685.html