Objective-C 运行时版本会为用户保留关联引用吗? [英] Will Objective-C runtime release retained associative references for user?

查看:57
本文介绍了Objective-C 运行时版本会为用户保留关联引用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一些像这样的代码时:

When some codes like this:

objc_setAssociatedObject (
  obj,
  &key,
  val,
  OBJC_ASSOCIATION_RETAIN
);

我需要打电话给相关人员吗

Do I need to call related

objc_setAssociatedObject (
  obj,
  &key,
  nil,
  OBJC_ASSOCIATION_RETAIN
);

释放保留值?Objective-C 运行时会自动释放 dealloc 或其他地方的关联引用吗?

to release the retained value? Does Objective-C runtime auto release the associative references in dealloc or somewhere?

推荐答案

当父对象被释放时,关联的对象将被释放.请参阅文档 这里,特别是这个例子:

Associated objects will be released when the parent object is deallocated. See the documentation here, specifically this example:

在第 1 点,字符串 overview 仍然有效,因为 OBJC_ASSOCIATION_RETAIN 策略指定数组保留关联对象.然而,当数组被释放时(在第 2 点),overview 被释放,因此在这种情况下也被释放.

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.

这篇关于Objective-C 运行时版本会为用户保留关联引用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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