什么是 objecc_setAssociatedObject() ? 在什么情况下应该使用它?

在我承担的一个项目中,原作者选择使用 objc_setAssociatedObject(),我不是100% 清楚它是做什么或为什么他们决定使用它。

我决定查一下,不幸的是,这些文件没有详细描述它的用途。

Objecc _ setAssociatedObject
使用给定键和关联策略为给定对象设置关联值。
void objc_setAssociatedObject(id object, void *key, id value, objc_AssociationPolicy policy)
参数
object
关联的源对象。
key
协会的钥匙。
value
要与对象的键关联的值。传递 nil 以清除现有的关联。
policy
关联的策略。有关可能的值,请参见“关联对象行为”

那么这个函数到底是做什么的,在什么情况下应该使用它呢?


阅读答案后编辑

那么以下代码的意义是什么呢?

Device *device = [self.list objectAtIndex:[indexPath row]];
DeviceViewController *next = [[DeviceViewController alloc] initWithController:self.controller
device:device
item:self.rootVC.selectedItem];
objc_setAssociatedObject(device, &kDeviceControllerKey, next, OBJC_ASSOCIATION_RETAIN);

如果设备已经是一个实例变量,那么把它和视图控制器联系起来有什么意义呢?

28765 次浏览

From the reference documents on Objective-C Runtime Reference:

You use the Objective-C runtime function objc_setAssociatedObject to make an association between one object and another. The function takes four parameters: the source object, a key, the value, and an association policy constant. The key is a void pointer.

  • The key for each association must be unique. A typical pattern is to use a static variable.
  • The policy specifies whether the associated object is assigned,
    retained, or copied, and whether the
    association is be made atomically or
    non-atomically. This pattern is
    similar to that of the attributes of
    a declared property (see “Property
    Declaration Attributes”). You specify the policy for the relationship using a constant (see
    objc_AssociationPolicy and
    Associative Object Behaviors).

Establishing an association between an array and a string

static char overviewKey;






NSArray *array =


[[NSArray alloc] initWithObjects:@"One", @"Two", @"Three", nil];


// For the purposes of illustration, use initWithFormat: to ensure


// the string can be deallocated


NSString *overview =


[[NSString alloc] initWithFormat:@"%@", @"First three numbers"];






objc_setAssociatedObject (


array,


&overviewKey,


overview,


OBJC_ASSOCIATION_RETAIN


);






[overview release];


// (1) overview valid


[array release];


// (2) overview invalid

At point 1, the string overview is still valid because the OBJC_ASSOCIATION_RETAIN policy specifies that the array retains the associated object. When the array is deallocated, however (at point 2), overview is released and so in this case also deallocated. If you try to, for example, log the value of overview, you generate a runtime exception.

objc_setAssociatedObject adds a key value store to each Objective-C object. It lets you store additional state for the object, not reflected in its instance variables.

It's really convenient when you want to store things belonging to an object outside of the main implementation. One of the main use cases is in categories where you cannot add instance variables. Here you use objc_setAssociatedObject to attach your additional variables to the self object.

When using the right association policy your objects will be released when the main object is deallocated.

To answer your revised question:

What is the point in associating the device with the view controller if it's already an instance variable?

There are several reasons why you might want to do this.

  • the Device class doesn't have a controller instance variable, or property and you can't change it or subclass it e.g. you don't have the source code.
  • you want two controllers associated with the device object and you can't change the device class or subclass it.

Personally, I think it is very rare to need to use low level Objective-C runtime functions. This looks like a code smell to me.

Here is a list of use cases for object associations:

one: To add instance variables to categories. In general this technique is advised against, but here is an example of a legitimate use. Let's say you want to simulate additional instance variables for objects you cannot modify (we are talking about modifying the object itself, ie without subclassing). Let's say setting a title on a UIImage.

// UIImage-Title.h:
@interface UIImage(Title)
@property(nonatomic, copy) NSString *title;
@end


// UIImage-Title.m:
#import <Foundation/Foundation.h>
#import <objc/runtime.h>


static char titleKey;


@implementation UIImage(Title)
- (NSString *)title
{
return objc_getAssociatedObject(self, &titleKey);
}


- (void)setTitle:(NSString *)title
{
objc_setAssociatedObject(self, &titleKey, title, OBJC_ASSOCIATION_COPY);
}
@end

Also, here is a pretty complex (but awesome) way of using associated objects with categories.. it basically allows you to pass in a block instead of a selector to a UIControl.


two: Dynamically adding state information to an object not covered by its instance variables in conjunction with KVO.

The idea is that your object gains state information only during runtime (ie dynamically). So the idea is that although you can store this state info in an instance variable, the fact that you're attaching this info into a an object instantiated at runtime and dynamically associating it with the other object, you are highlighting the fact that this is a dynamic state of the object.

One excellent example that illustrates this is this library, in which associative objects are used with KVO notifications. Here is an excerpt of the code (note: this KVO notification isn't necessary to run make the code in that library work.. rather it's put there by the author for convenience, basically any object that registers to this will be notified via KVO that changes have happened to it):

static char BOOLRevealing;


- (BOOL)isRevealing
{
return [(NSNumber*)objc_getAssociatedObject(self, &BOOLRevealing) boolValue];
}


- (void)_setRevealing:(BOOL)revealing
{
[self willChangeValueForKey:@"isRevealing"];
objc_setAssociatedObject(self, &BOOLRevealing,
[NSNumber numberWithBool:revealing], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self didChangeValueForKey:@"isRevealing"];
}

bonus: take a look at this discussion/explanation of associated objects by Mattt Thompson, author of the seminal AFNetworking library