【原】iOS动态性(一):动态添加属性的方法——关联(e.g. 向Category添加属性)

想到要如何为所有的对象增加实例变量吗?我们知道,使用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进行属性补充,添加FlashColor属性。一般我们有个原则:能用category扩展就不用继承,因为随着继承深度的增加,代码的可维护性也会增加很多。使用category可以这么做:

#import <UIKit/UIKit.h>
#import <objc/runtime.h>

@interface UILabel (Associate)//单单从头文件看是不是很像一个类?再看看.m文件你就知道这些都是假象了

- (nonatomic, retain) UIColor *FlashColor;

@end
#import "UILabel+Associate.h"

@implementation UILabel (Associate)

@dynamic FlashColor; 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是一样的,不再赘述

3、在implement开始处的@dynamic声明。一般来说@dynamic与@synthesize都可以用来声明属性,@synthesize是默认的声明,意思是编译器在编译阶段自动为你的属性生成setter与getter;而@dynamic则告诉编译器,别慌,小子,编译阶段不用为我生成setter与getter,在runtime我已经自己实现了setter与getter。此处我们选择@dynamic。事实上,二者曾引起stackOverFlow上强烈的争论:请点这里

下面我们再来看另一个例子,来源于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";
	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;
}

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

原创文章,转载请注明 编程小翁@博客园,邮件zilin_weng@163.com,欢迎各位与我在C/C++/Objective-C/机器视觉等领域展开交流!

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

原文地址:https://www.cnblogs.com/wengzilin/p/4331685.html