NSArray 对 ARC 下对象的弱引用 (__unsafe_unretained)
我需要在 NSArray 中存储对对象的弱引用,以防止保留循环。我不确定要使用的正确语法。这是正确的方法吗?
Foo* foo1 = [[Foo alloc] init];
Foo* foo2 = [[Foo alloc] init];
__unsafe_unretained Foo* weakFoo1 = foo1;
__unsafe_unretained Foo* weakFoo2 = foo2;
NSArray* someArray = [NSArray arrayWithObjects:weakFoo1, weakFoo2, nil];
请注意,我需要支持 iOS 4.x,因此使用 __unsafe_unretained
而不是 __weak
。
编辑(2015-02-18):
对于那些想要使用真正的__weak
指针(而不是__unsafe_unretained
)的人,请查看这个问题: ARC下清零弱引用集合
I need to store weak references to objects in an NSArray, in order to prevent retain cycles. I'm not sure of the proper syntax to use. Is this the correct way?
Foo* foo1 = [[Foo alloc] init];
Foo* foo2 = [[Foo alloc] init];
__unsafe_unretained Foo* weakFoo1 = foo1;
__unsafe_unretained Foo* weakFoo2 = foo2;
NSArray* someArray = [NSArray arrayWithObjects:weakFoo1, weakFoo2, nil];
Note that I need to support iOS 4.x, thus the __unsafe_unretained
instead of __weak
.
EDIT (2015-02-18):
For those wanting to use true __weak
pointers (not __unsafe_unretained
), please check out this question instead: Collections of zeroing weak references under ARC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
正如 Jason 所说,你不能让 NSArray 存储弱引用。实现 Emile 建议将一个对象包装在另一个存储对它的弱引用的对象中的最简单方法如下:
另一个选项:类别 使 NSMutableArray 可以选择性地存储弱引用。
请注意,这些是“不安全的未保留”引用,而不是自清零弱引用。如果在对象被释放后数组仍然存在,那么您将拥有一堆垃圾指针。
As Jason said, you can't make
NSArray
store weak references. The easiest way to implement Emile's suggestion of wrapping an object inside another object that stores a weak reference to it is the following:Another option: a category that makes
NSMutableArray
optionally store weak references.Note that these are "unsafe unretained" references, not self-zeroing weak references. If the array is still around after the objects are deallocated, you'll have a bunch of junk pointers.
使用 NSValue 帮助器或创建集合(数组、集合、字典)对象并禁用其的解决方案对于使用 ARC,保留/释放回调都不是 100% 的故障安全解决方案。
正如对这些建议的各种评论指出的那样,此类对象引用不会像真正的弱引用那样工作:
ARC 支持的“正确”弱属性有两种行为:
现在,虽然上述解决方案符合行为#1,但它们不表现出行为#2。
为了也获得行为#2,您必须声明您自己的辅助类。它只有一个弱属性来保存您的参考。然后将此辅助对象添加到集合中。
哦,还有一件事:iOS6 和 OSX 10.8 据说提供了更好的解决方案:
这些应该为您提供保存弱引用的容器(但请注意下面 Matt 的评论)。
示例(更新于 2022 年 2 月 2 日)
如果您运行此示例,您会发现将
TestClass
对象添加到指针数组pa
中,然后再次释放该对象后,指针(内部是一个弱对象引用)现在根据需要设置为 null。但是,请注意,最后调用
[pa Compact]
不会像我预期的那样删除 nil 指针。The solutions to use a NSValue helper or to create a collection (array, set, dict) object and disable its Retain/Release callbacks are both not 100% failsafe solutions with regard to using ARC.
As various comments to these suggestions point out, such object references will not work like true weak refs:
A "proper" weak property, as supported by ARC, has two behaviors:
Now, while the above solutions will comply with behavior #1, they do not exhibit #2.
To get behavior #2 as well, you have to declare your own helper class. It has just one weak property for holding your reference. You then add this helper object to the collection.
Oh, and one more thing: iOS6 and OSX 10.8 supposedly offer a better solution:
These should give you containers that hold weak references (but note matt's comments below).
An example (updated 2 Feb 2022)
If you run this you'll find that after adding the
TestClass
object to the pointer arraypa
, then releasing that object again, the pointer (which is internally a weak object ref) is now set to null as desired.However, note that calling
[pa compact]
at the end will not remove the nil pointer as I'd have expected.在编写 C++ 20 年之后,我是 Objective-C 的新手。
在我看来,objective-C 在松散耦合消息传递方面非常出色,但在数据管理方面却很糟糕。
想象一下,当我发现 xcode 4.3 支持 Objective-C++ 时,我是多么高兴!
所以现在我将所有 .m 文件重命名为 .mm(编译为 Objective-C++)并使用 C++ 标准容器进行数据管理。
因此,“弱指针数组”问题变成了 __weak 对象指针的 std::vector:
这相当于 c++ 习惯用法:
概念证明(考虑到 Tommy 对向量重新分配的担忧):
main.mm:
示例日志输出:
I am new to objective-C, after 20 years of writing c++.
In my view, objective-C is excellent at loosely-coupled messaging, but horrible for data management.
Imagine how happy I was to discover that xcode 4.3 supports objective-c++!
So now I rename all my .m files to .mm (compiles as objective-c++) and use c++ standard containers for data management.
Thus the "array of weak pointers" problem becomes a std::vector of __weak object pointers:
Which is equivalent to the c++ idiom:
Proof of concept (in the light of Tommy's concerns about vector reallocation):
main.mm:
example log output:
如果您不需要特定的顺序,您可以使用带有特殊键/值选项的
NSMapTable
If you do not require a specific order you could use
NSMapTable
with special key/value options我相信最好的解决方案是使用 NSHashTable 或 NSMapTable。键或/和值可以很弱。您可以在这里阅读更多相关信息:http://nshipster.com/nshashtable-and-nsmaptable/
I believe the best solution for this is to use NSHashTable or NSMapTable. the Key or/and the Value can be weak. You can read more about it here: http://nshipster.com/nshashtable-and-nsmaptable/
要将弱自引用添加到 NSMutableArray,请创建一个具有弱属性的自定义类,如下所示。
步骤3:稍后,如果属性
delegateWeakReference == nil
,则可以从数组中删除该对象该属性将为nil,并且引用将在适当的时间被释放,独立于该数组引用
To add weak self reference to NSMutableArray, create a custom class with a weak property as given below.
Step 3: later on, if the property
delegateWeakReference == nil
, the object can be removed from the arrayThe property will be nil, and the references will be deallocated at proper time independent of this array references
最简单的解决方案:
注意: 这也适用于 iOS 4.x。
The simplest solution:
Note: And this works on iOS 4.x too.
不,那是不正确的。这些实际上并不是弱引用。您现在无法真正将弱引用存储在数组中。您需要有一个可变数组,并在使用完它们后删除引用,或者在使用完后删除整个数组,或者滚动您自己的支持它的数据结构。
希望他们能在不久的将来解决这个问题(
NSArray
的弱版本)。No, that's not correct. Those aren't actually weak references. You can't really store weak references in an array right now. You need to have a mutable array and remove the references when you're done with them or remove the whole array when you're done with it, or roll your own data structure that supports it.
Hopefully this is something that they'll address in the near future (a weak version of
NSArray
).我刚刚遇到了同样的问题,发现我的 before-ARC 解决方案在按照设计使用 ARC 进行转换后可以正常工作。
输出:
使用 iOS 版本 4.3、5.1、6.2 检查。
希望它对某人有用。
I've just faced with same problem and found that my before-ARC solution works after converting with ARC as designed.
Output:
Checked with iOS versions 4.3, 5.1, 6.2.
Hope it will be useful to somebody.
如果您需要归零弱引用,请参阅此答案以获取可用于包装类的代码。
该问题的其他答案建议使用基于块的包装器,以及方法自动从集合中删除归零的元素。
If you need zeroing weak references, see this answer for code you can use for a wrapper class.
Other answers to that question suggest a block-based wrapper, and ways to automatically remove zeroed elements from the collection.
如果你经常使用这个行为,它会指示你自己的 NSMutableArray 类(NSMutableArray 的子类),它不会增加保留计数。
你应该有这样的东西:
If you use a lot this comportment it's indicated to your own NSMutableArray class (subclass of NSMutableArray) which doesn't increase the retain count.
You should have something like this:
我认为 Erik Ralston 先生在他的 Github 存储库 https://gist.github 上提出了一个优雅的解决方案
。 com/eralston/8010285
这是基本步骤:
为 NSArray 和 NSMutableArray 创建一个类别,
在实现中 创建一个具有弱属性的便利类。您的类别会将对象分配给这个弱属性。
.h.m
I think an elegant solution is what Mr. Erik Ralston propose on his Github repository
https://gist.github.com/eralston/8010285
this are the essential steps:
create a category for NSArray and NSMutableArray
in the implementation create a convenience class with a weak property. Your category will assign the objects to this weak property.
.h
.m