什么是objc_setAssociatedObject()以及在什么情况下应该使用它? [英] What is objc_setAssociatedObject() and in what cases should it be used?

查看:201
本文介绍了什么是objc_setAssociatedObject()以及在什么情况下应该使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我接受的一个项目中,原作者选择使用 objc_setAssociatedObject()而我并不是100%明白它的作用或为什么他们决定使用它。

In a project I have taken on, the original author has opted to use objc_setAssociatedObject() and I'm not 100% clear what it does or why they decided to use it.

我决定查阅它,不幸的是,文档对其目的并不十分具有描述性。

I decided to look it up and, unfortunately, the docs aren't very descriptive about its purpose.

objc_setAssociatedObject

使用给定的密钥和关联策略为给定对象设置关联值。

void objc_setAssociatedObject (id object,void * key,id value,objc_AssociationPolicy policy)

参数

object

关联的源对象。

key

关联的密钥。

value

与对象的密钥关联的值。传递nil以清除现有关联。

策略

关联的策略。有关可能的值,请参阅关联对象行为。

objc_setAssociatedObject
Sets an associated value for a given object using a given key and association policy.
void objc_setAssociatedObject(id object, void *key, id value, objc_AssociationPolicy policy)
Parameters
object
The source object for the association.
key
The key for the association.
value
The value to associate with the key key for object. Pass nil to clear an existing association.
policy
The policy for the association. For possible values, see "Associative Object Behaviors."

那么这个函数究竟做了什么以及在什么情况下应该使用它?

So what exactly does this function do and in what cases should it be used?

阅读答案后编辑

那么有什么意义呢?以下代码?

So what is the point in the following code?

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);

如果设备已经是实例变量,那么将设​​备与视图控制器相关联的重点是什么?

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

推荐答案

来自 Objective-C运行时参考


使用Objective-C运行时
函数 objc_setAssociatedObject
在一个对象
和另一个对象之间建立关联。该函数需要四个
参数:源对象,一个键,
值和一个关联策略
常量。键是一个void指针。

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.


  • 每个关联的键必须是唯一的。典型的模式是
    使用静态变量。

  • 策略指定是否分配关联对象,保留或复制
    ,以及是否

    关联是以原子方式进行的,或者是非原子的
    。这个模式是

    ,类似于声明属性的
    属性(参见Property

    声明属性)。您使用
    a常量指定
    关系的策略(请参阅
    objc_AssociationPolicy和

    关联对象行为)。

在数组和字符串之间建立关联

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




在第1点,字符串概述是
仍然有效,因为
OBJC_ASSOCIATION_RETAIN策略
指定数组保留
关联对象。当数组是
deallocated时,(在第2点),
概述被释放,因此在这个
的情况下也被释放。如果您尝试以
为例,记录
概述的值,则会生成运行时
异常。

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()以及在什么情况下应该使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆