Objective-C中的弱键字典 [英] Weak-keyed dictionary in Objective-C

查看:118
本文介绍了Objective-C中的弱键字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能与Objective-C中带有弱键的ActionScript 3的 Dictionary 对象类似。我希望能够将一个类的实例附加到其他任意实例。

I'm wondering if it's possible to have something similar to ActionScript 3's Dictionary object with weak keys in Objective-C. I want to be able to 'attach' an instance of a class to other arbitrary instances.

示例;

MetaData *meta = [MetaData metaDataForObject:someObject];
meta.whatever = foo;

后来:

foo = [MetaData metaDataForObject:someObject].whatever;
[foo doStuff];

棘手的部分是在 someObject 已取消分配,我希望释放 meta 引用的对象(并取消分配,假设没有客户端代码保留它)。

The tricky part is that after the objected referenced by someObject is dealloc'd, I want the object referenced by meta to be released (and dealloc'd, assuming no client code has retained it).

可能吗?我看了一下 + [NSValue valueWithNonretainedObject:] ,但是我不确定这是否是我想要的,因为稍后我查询-[ NSValue nonretainedObjectValue] 看来我会得到一个指向垃圾的指针(当对象被解除分配时,NSValue如何将指针归零?)。

Possible? I took a look at +[NSValue valueWithNonretainedObject:] but I'm not sure if this is what I want because there when I later query -[NSValue nonretainedObjectValue] it seems like I would get a pointer to garbage (how could NSValue zero the pointer when the object was dealloc'd?).

谢谢

本杰明

2011年9月23日更新:做到这一点的方法是使用 objc_setAssociatedObject 和相关函数。参见 Objective-C运行时参考

Update 23 September 2011: I believe the way to do this is with objc_setAssociatedObject and related functions. See the Objective-C Runtime Reference.

推荐答案

听起来您要的是对要释放的弱引用实例变量做出反应。您当然可以使用 __ weak 属性(启用GC)来创建弱引用,但是没有内置的机制可以在目标达到目标值归零后进行捕获。

It sounds like what you're asking for is the ability to react to a weak-referenced instance variable being deallocated. You can certainly use the __weak attribute (with GC enabled) to create a weak reference, but there's no built-in mechanism to catch when such an attribute is zeroed after its target is GCed.

如果您确实想要这样做,最好的选择是使用Apple的Key-Value Observing使用的相同机制:方法混乱。维护将对象映射到其相应元数据对象的全局表(例如 NSHashMap NSMapTable ),然后替换 dealloc / finalize 方法,该对象是您要附加的对象类中的版本,这些版本在其中查找相应的元数据对象桌子并发送一条消息将其拆除。 (您还需要另一个或两个表,这些表将类映射到其原始的 dealloc / finalize 方法。) JRSwizzle 提供了一个很好的界面,可以使人陷入混乱。

If you really want this, your best bet is to use the same mechanism Apple's Key-Value Observing uses: method swizzling. Maintain a global table (such as a NSHashMap or NSMapTable) mapping objects to their corresponding metadata objects, then replace the dealloc/finalize methods in the class of the object you're attaching to with versions that look up the corresponding metadata object in the table and send a message to tear it down. (You also need another table or two that maps classes to their original dealloc/finalize methods.) JRSwizzle provides a nice interface to swizzling.

如果您想真正成为幻想,而不是覆盖目标类的所有对象的 dealloc / finalize ,可以创建一个代理类,并为该类重新分配 isa 指针,这样,对于您不在观看的对象,释放不会对性能造成任何影响。 (KVO也这样做。)

If you want to to be really fancy, instead of overwriting the dealloc/finalize for all objects of the target class, you can create a proxy class and reassign the isa pointer for just that class, such that there's no performance hit on deallocation for the objects you're not watching. (KVO does this, too.)

这篇关于Objective-C中的弱键字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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