关联objc_setAssociatedObject,objc_getAssociatedObject
2014-07-21 15:19
288 查看
关联
关联是指把两个对象相互关联起来,使得其中的一个对象作为另外一个对象的一部分。关联特性只有在Mac OS X V10.6以及以后的版本上才是可用的。
在类的定义之外为类增加额外的存储空间
使用关联,我们可以不用修改类的定义而为其对象增加存储空间。这在我们无法访问到类的源码的时候或者是考虑到二进制兼容性的时候是非常有用。关联是基于关键字的,因此,我们可以为任何对象增加任意多的关联,每个都使用不同的关键字即可。关联是可以保证被关联的对象在关联对象的整个生命周期都是可用的(在垃圾自动回收环境下也不会导致资源不可回收)。
创建关联
创建关联要使用到Objective-C的运行时函数:objc_setAssociatedObject来把一个对象与另外一个对象进行关联。该函数需要四个参数:源对象,关键字,关联的对象和一个关联策略。当然,此处的关键字和关联策略是需要进一步讨论的。■ 关键字是一个void类型的指针。每一个关联的关键字必须是唯一的。通常都是会采用静态变量来作为关键字。
■ 关联策略表明了相关的对象是通过赋值,保留引用还是复制的方式进行关联的;还有这种关联是原子的还是非原子的。这里的关联策略和声明属性时的很类似。这种关联策略是通过使用预先定义好的常量来表示的。
//唯一静态变量key
static const char xxxxxx;
objc_setAssociatedObject(alert, &associatedkey, message,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
获取相关联的对象
获取相关联的对象时使用Objective-C函数objc_getAssociatedObject。objc_getAssociatedObject(alertView, &associatedkey);
断开关联
断开关联是使用objc_setAssociatedObject函数,传入nil值即可。接着列表7-1中的程序,我们可以使用如下的代码来断开字符串overview和arry之间的关联:
objc_setAssociatedObject(alertView, &associatedkey,nil,OBJC_ASSOCIATION_ASSIGN);
引用下别人的一段程序:
int main(int argc, const char* argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool] alloc init]; static char overviewKey; NSArray *array =[[NSArray alloc] initWidthObjects:@"One", @"Two", @"Three", nil]; //为了演示的目的,这里使用initWithFormat:来确保字符串可以被销毁 NSString * overview = [[NSString alloc] initWithFormat:@"@",@"First three numbers"]; objc_setAssociatedObject(array, &overviewKey, overview, OBJC_ASSOCIATION_RETAIN); [overview release]; NSString *associatedObject = (NSString *)objc_getAssociatedObject(arrray, &overviewKey); NSLog(@"associatedObject:%@", associatedObject); objc_setAssociatedObject(array, &overviewKey, nil, OBJC_ASSOCIATION_ASSIGN); [array release]; [pool drain]; return 0; }
使用时
#import <objc/runtime.h>
//objc_setAssociatedObject需要四个参数:源对象,关键字,关联的对象和一个关联策略。
//1 源对象alert
//2 关键字 唯一静态变量key associatedkey
//3 关联的对象 sender
//4 关键策略 OBJC_ASSOCIATION_RETAIN_NONATOMIC
参考:http://blog.csdn.net/lengshengren/article/details/16886915
前篇介绍
相关文章推荐
- [IOS] 关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- iOS 关联objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects
- [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- 关联 objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects
- IOS学习 关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObject)
- [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- 关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- 关联(objc_setAssociatedObject、objc_getAssociatedObject)
- [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- 转: [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- 【iOS开发技术】对象关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)