[Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
来源:互联网 发布:微信数据无法修复 编辑:程序博客网 时间:2024/06/03 17:57
关联特性只有在Mac OS X V10.6以及以后的版本上才是可用的。
在类的定义之外为类增加额外的存储空间
使用关联,我们可以不用修改类的定义而为其对象增加存储空间。这在我们无法访问到类的源码的时候或者是考虑到二进制兼容性的时候是非常有用。关联是基于关键字的,因此,我们可以为任何对象增加任意多的关联,每个都使用不同的关键字即可。关联是可以保证被关联的对象在关联对象的整个生命周期都是可用的(在垃圾自动回收环境下也不会导致资源不可回收)。
创建关联
创建关联要使用到Objective-C的运行时函数:objc_setAssociatedObject来把一个对象与另外一个对象进行关联。该函数需要四个参数:源对象,关键字,关联的对象和一个关联策略。当然,此处的关键字和关联策略是需要进一步讨论的。■ 关键字是一个void类型的指针。每一个关联的关键字必须是唯一的。通常都是会采用静态变量来作为关键字。
■ 关联策略表明了相关的对象是通过赋值,保留引用还是复制的方式进行关联的;还有这种关联是原子的还是非原子的。这里的关联策略和声明属性时的很类似。这种关联策略是通过使用预先定义好的常量来表示的。
下面的代码展示了如何把一个字符串关联到一个数组上。
- 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];
- //(1) overview仍然是可用的
- [array release];
- //(2)overview 不可用
获取相关联的对象
获取相关联的对象时使用Objective-C函数objc_getAssociatedObject。接着上面列表7-1的代码,我们可以使用如下代码来获取与array相关联的字符串:
NSString *associatedObject = (NSString *)objc_getAssociatedObject(array, &oveviewKey);
断开关联
断开关联是使用objc_setAssociatedObject函数,传入nil值即可。
接着列表7-1中的程序,我们可以使用如下的代码来断开字符串overview和arry之间的关联:
objc_setAssociatedObject(array, &overviewKey, nil, OBJC_ASSOCIATION_ASSIGN);
其中,被关联的对象为nil,此时关联策略也就无关紧要了。
使用函数objc_removeAssociatedObjects可以断开所有关联。通常情况下不建议使用这个函数,因为他会断开所有关联。只有在需要把对象恢复到“原始状态”的时候才会使用这个函数。
一个完整的实例程序
下面的程序综合了前面的代码.
- #import <Foundation/Foundation.h>
- #import <objc/runtime.h>
- 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;
- }
- [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)
- [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)
- [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)
- jieba分词和去停用词--中文分词工具
- 概率抽奖
- Spring注解详解
- vim 关键字字典自动补全
- Stub和Mock的区别
- [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- CentOS7 固定ip
- DiskLruCache磁盘缓存的使用介绍
- 十一月英语总结——英语需要每天都保持热情
- The type javax.servlet.http.HttpServletRequest cannot be resolved. It is indirectly referenced from
- Redis的异常采取的措施。(代码如下)
- 通过JS回调解决异步请求中断执行的问题
- python 3 TCP简单小程序
- WEB打印控件