问题描述
我想在 NSDictionary
中存储对象的归零弱引用。这是为了引用父 NSDictionary
,所以我可以在不搜索的情况下爬行大型结构。
I would like to store a zeroing weak reference to an object in a NSDictionary
. This is for a reference to a parent NSDictionary
, so I can crawl back up a large structure without searching.
我不能在这里使用 __ weak
;即使我的本地引用很弱, NSDictionary
也会存储对弱引用对象的强引用。当然, NSDictionary
不能有 nil
对象。
I can not use __weak
here; even if my local reference is weak, the NSDictionary
will store a strong reference to the object that was weakly referenced. And, of course, NSDictionary
can't have nil
objects.
我在iOS上,而不是Mac,所以 NSHashTable
不可用。我只希望一个物体变弱;其余部分应该仍然很强。
I'm on iOS, not Mac, so NSHashTable
isn't available. And I only want one object to be weak; the rest should still be strong.
(我将发布我的答案,所以如果没有更好的答案,我有一些标记可以接受。但我是希望有人有更好的答案。)
(I'm going to post my answer, so I have something to mark as accepted if there's no better answer. But I'm hoping someone has a better answer.)
推荐答案
在iOS 6+中你可以使用并选择是否希望对象和/或键变弱或变强。
In iOS 6+ you can use NSMapTable and choose if you want objects and/or keys to be weak or strong.
这篇关于NSDictionary中的弱对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!