有时候需要在对象中存放相关信息,我们通常会从对象所属的类中继承一个子类然后改用这个子类对象,但是并非所有的情况都能够这么做。有时候类的实例可能由于某种机制而创建,但是开发者无法令这种机制创建自己所需要的子类实例,所以Objective—C中有一项强大的特性可以解决此问题,这就是“关联对象”。
Objective—C作为一门动态语言,它本身有一个非常大的弱点,即不能在类的category中方便地为类添加新的自定义属性。「关联对象」(Associated Objects)或「关联引用」(Associative References)是基于Objective-C 2.0的一个Runtime特性,它使得可以在Runtime为某个类对象绑定一个对象(通过简单的封装,可以让这个关联属性在使用上和普通属性有类似的体验)
关联引用的最主要应用场景就是在category中为类动态添加属性.也可以在需要的时候进行存储数据。
在既有的类中使用关联对象存放自定义数据
1:关联对象: 可以给某对象许多的其他对象,这些对象通过键来区分.存处对象值的时候,可以指明”存储策略”,用以维护相应的”内存管理语义”.如下所示:
typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) {
OBJC_ASSOCIATION_ASSIGN = 0, /**< Specifies a weak reference to the associated object. */
OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object.
* The association is not made atomically. */
OBJC_ASSOCIATION_COPY_NONATOMIC = 3, /**< Specifies that the associated object is copied.
* The association is not made atomically. */
OBJC_ASSOCIATION_RETAIN = 01401, /**< Specifies a strong reference to the associated object.
* The association is made atomically. */
OBJC_ASSOCIATION_COPY = 01403 /**< Specifies that the associated object is copied.
* The association is made atomically. */
};
2:下列方法管理关联对象
该方法以给定的键和策略为某对象设置关联对象值.
void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
该方法根据给定的键从某对象中获取相应的关联对象值.
void objc_getAssociatedObject(id object, const void *key)
该方法移除指定对象的全部关联对象.
void objc_removeAssociatedObjects(id object)
我们可以把某对象想象成为NSDictionary, 把关联到该对象的值理解为字典中的条目,于是,存取关联对象的值就相当于在 NSDictionary对象上调用[obect setObject:id forKey:id]与 [obect objectForKey:id]方法.然而两者之间有一个重要的差别:设置关联对象时用的键(key)是个”不透明的指针”.如果在两个键上调用”isEqual”方法返回的是 YES, 那么NSDictionary就认为两者相等.然而在设置关联对象值时,若想令两个键匹配到同一个值,则两者必须是完全相同的指针才行,所以在设置关联对象值时,通常使用静态全局变量做键.
3:关联对象的使用《Effective Objective-C 2.0》例子:
- (void)askUserAQuestion {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Question"
message:@"What do you want to do?"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Continue", nil];
[alertView show];
}
// UIAlertViewDelegate protocol method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
[self doCancel];
} else {
[self doContinue];
}
}
但是,如果在同一个ViewController中需要多次使用UIAlertView,那么UIAlertViewDelegate方法- (void)alertView:clickedButtonAtIndex:;的实现代码就变得复杂了,可能这样写:
// UIAlertViewDelegate protocol method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView == self.AlertOne) {
if (buttonIndex == 0) {
} else {
}
} else if (alertView == self.AlertTwo) {
if (buttonIndex == 0) {
} else {
}
} else if (alertView == AlertThree) {
if (buttonIndex == 0) {
} else {
}
}
}
所以要是在创建UIAlertView对象时就把相关的处理逻辑写好,那就简单多了。这可以通过关联对象来处理。简单来说,就是创建UIAlertView对象完成后,对其关联一个block,而在alertView:clickedButtonAtIndex:调用这个block即可。此方案的实现代码如下:
static void *alertViewBlockKey = &alertViewBlockKey;
- (void)askUserAQuestion {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Question"
message:@"What do you want to do?"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Continue", nil];
void (^block)(NSInteger) = ^(NSInteger buttonIndex) {
if (buttonIndex == 0) {
[self doCancel];
} else {
[self doContinue];
}
};
objc_setAssociatedObject(alertView, alertViewBlockKey, block, OBJC_ASSOCIATION_COPY);
[alertView show];
}
// UIAlertViewDelegate protocol method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
void (^block)(NSInteger) = objc_getAssociatedObject(alertView, alertViewBlockKey);
block(buttonIndex);
}
使用这种方式改写之后,创建警告视图与处理操作结果的代码都放在一起了,这样比原来更易读懂,因为我们不需要在两部分代码之间进行来回查看。但是也需要注意,块可能捕获某些变量,这也许会造成循环引用。所以,除非确实需要这么做,还是少使用,不然代码可能很难调试。
4:延伸阅读:
http://nshipster.cn/associated-objects/
http://blog.leichunfeng.com/blog/2015/06/26/objective-c-associated-objects-implementation-principle/
Objective-C 的关联对象是一种Runtime特性,允许在运行时为类对象动态添加属性,尤其适用于category中添加自定义属性。关联对象通过键值对存储,并可指定存储策略以维护内存管理。使用静态全局变量作为键确保唯一性。关联对象常用于简化复杂的 Delegate 模式,如将处理逻辑以 Block 形式关联到 UIAlertView,提高代码可读性,但要注意可能引发的循环引用问题。
1572

被折叠的 条评论
为什么被折叠?



