Objective-C——关联对象
动态语言
OC是一种动态语言,它的方法,对象的类型都是到运行的时候才能够确定的。所以这就使得OC存在了关联对象这一强大的机制。
关联对象
所谓关联对象,其实就是我们在运行时对一个已存在的对象上面绑定一个对象,使两个对象变成动态的聚合关系。
关联对象和属性一样有着关键字,以下是关联对象的存储策略:
关联类型 | 等效的@property属性 |
OBJC_ASSOCIATION_ASSIGN | assign |
OBJC_ASSOCIATION_RETAIN_NONATOMIC | nonatomic,retain |
OBJC_ASSOCIATION_COPY_NONATOMIC | nonatomic,copy |
OBJC_ASSOCIATION_RETAIN | retain |
OBJC_ASSOCIATION_COPY | copy |
关联对象主要靠下面三个函数,它们都位于<objc/runtime>下
void objc_setAssociatedObject(id object, void *key ,id value ,objc_AssociationPolicy policy)
设置关联对象
参数 | 说明 |
object | 要进行关联的对象 |
key | 一个内存表示,在比较两个关联对象是否相等时,比较的就是内存地址,所以一般用一个全局静态变量表示 |
value | 被关联的对象 |
policy | 存储策略 |
id objc_getAssociatedObject(id object, void *key)
获取关联对象
void objc_removeAssociatedObjects(id object)
删除关联对象
作用
关联对象一般用于动态的扩展一个对象,但是这一般都是在其他方法不行的事后才会使用,因为关联对象很可能会出现难以查找的bug。
关联对象有时也会用于在category向类添加属性,这点等会儿在分析。
下面Demo在UIAlertView上面动态绑定了一个block,把按钮处理逻辑和alert调用整合在了一起。
static char *AlertKey = "alertKey"; - (void)viewDidLoad {
[super viewDidLoad]; alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"other", nil];
} - (IBAction)click:(id)sender { void (^block) (NSInteger) = ^(NSInteger buttonIndex){
if (buttonIndex == ){
NSLog(@"click cancel");
}
else{
NSLog(@"click other");
}
};
objc_setAssociatedObject(alert, AlertKey, block, OBJC_ASSOCIATION_COPY);
[alert show];
} - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
void (^block)(NSInteger) = objc_getAssociatedObject(alertView, AlertKey);
block(buttonIndex);
}
运行程序点击两个按钮分别输出如下
-- ::27.146 Dynamic[:] click other
-- ::28.262 Dynamic[:] click cancel
接下来我们给UIViewController在category中添加一个addition属性
#import "ViewController.h"
#import <objc/runtime.h> @interface UIViewController (Addition) @property(nonatomic ,copy) NSString *addition; @end
#import "UIViewController+Addition.h" static const void *IndieBandNameKey = &IndieBandNameKey; @implementation UIViewController (Addition) -(void)setAddition:(NSString *)addition{
objc_setAssociatedObject(self, IndieBandNameKey, addition, OBJC_ASSOCIATION_COPY);
} -(NSString *)addition{
return objc_getAssociatedObject(self, IndieBandNameKey);
} @end
这里说明一下,这里关联的实际上是形参addition,和属性没有什么关系,写@property 就是为了能够使用‘.’语法。但是@porperty里面的属性存储策略还是要和关联对象一致的,这样不容易造成误解。
所以每次setAddition实际上我们并不是修改了原有内存的值,而是改变了指针指向的地址,这里需要注意一下。
然后修改刚才alert的代码
- (void)viewDidLoad {
[super viewDidLoad]; alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"other", nil];
alert.delegate = self;
self.addition = @"addition"; } - (IBAction)click:(id)sender { void (^block) (NSInteger) = ^(NSInteger buttonIndex){
if (buttonIndex == ){
NSLog(@"click cancel");
objc_removeAssociatedObjects(self);
}
else{
NSLog(@"click other");
NSLog(@"%@",self.addition);
}
};
objc_setAssociatedObject(alert, AlertKey, block, OBJC_ASSOCIATION_COPY);
[alert show];
} - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
void (^block)(NSInteger) = objc_getAssociatedObject(alertView, AlertKey);
block(buttonIndex);
}
注意三条加粗的语句,然后我们运行看结果
-- ::54.353 Dynamic[:] click other
-- ::54.353 Dynamic[:] addition
-- ::55.804 Dynamic[:] click cancel
-- ::57.491 Dynamic[:] click other
-- ::57.491 Dynamic[:] (null)
首先我们使用了关联对象,所以点击other的时候会看到打印出了addition。点击cancel的时候又因为我们remove了关联对象,此时再点击other的时候addition就变成null了。
最新文章
- cakephp之查询
- 内核源码分析之进程调度机制(基于3.16-rc4)
- 第十七章 委托 第十八章 Attribute 第十九章 可空值类型
- 每个项目单独配置 git 用户
- linux指定动态运行库的位置
- Twitter License for Android
- BCM wifi分析
- Prism之使用EventAggregation进行模块间通信
- Jmeter之接口测试
- SpringMVC处理请求和返回流程
- dl,dt,dd标签的使用
- PTA L2-4 关于堆的判断
- 先序遍历DOM树的5种方法
- saltstack grains
- 201621123002《Java程序设计》第七周学习总结
- 动态改变APP图标
- linux下VLAN设置
- 黄聪:AngularJS中的$resource使用与Restful资源交互(转)
- Linux下设置固定IP的方法
- ExtJS学习笔记2:响应事件、使用AJAX载入数据
热门文章
- windows下通过navicat for mysql连接centos6.3-64bit下的MySQL数据库
- 学PHP也要懂得HTML
- spring-cloud-starter-hystrix(断路器)服务不通或者调用失败后的错误处理和回调
- HDU 1133 Buy the Ticket 卡特兰数
- 1. 找出数组中的单身狗OddOccurrencesInArray Find value that occurs in odd number of elements.
- ZOJ 3827 Information Entropy(数学题 牡丹江现场赛)
- Android Message和obtainMessage的差别
- LeetCode 242. Valid Anagram (验证变位词)
- javascript设置和获取cookie的方法
- ios7--UIImageView