代表的非保留数组
在 Cocoa Touch 项目中,我需要一个特定的类来不仅拥有单个委托对象,而且拥有多个委托对象。
看起来我应该为这些委托创建一个 NSArray; 问题是 NSArray 会保留所有这些委托,但它不应该保留(按照惯例,对象不应保留其委托)。
我应该编写自己的数组类来防止保留还是有更简单的方法? 谢谢你!
In a Cocoa Touch project, I need a specific class to have not only a single delegate object, but many of them.
It looks like I should create an NSArray for these delegates;
the problem is that NSArray would have all these delegates retained, which it shouldn't (by convention objects should not retain their delegates).
Should I write my own array class to prevent retaining or are there simpler methods?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我不久前发现了这段代码(不记得该归属于谁)。
这是非常巧妙的,使用 Category 来允许创建一个可变数组,通过使用带有适当回调的
CFArray
支持它,该数组不会保留/释放。编辑找到原始文章:http://ofcodeandmen.poltras.com
I found this bit of code awhile ago (can't remember who to attribute it to).
It's quite ingenius, using a Category to allow the creation of a mutable array that does no retain/release by backing it with a
CFArray
with proper callbacks.EDIT Found the original article: http://ofcodeandmen.poltras.com
我提出了早期答案之一的重要限制,以及解释和改进。
Johnmph 建议使用
[NSValue valueWithNonretainedObject:]
。请注意,当您执行此操作时,您的引用在 NSValue 对象内部的行为不类似于
__weak
,而是类似于__unsafe_unretained
。更具体地说,当您尝试取回引用时(使用 [myNSValue nonretainedObjectValue]),如果该对象在此之前已被释放,您的应用程序将崩溃并出现 EXC_BAD_ACCESS 信号!换句话说,在 NSValue 对象内部,弱引用不会自动设置为 nil。这花了我几个小时才弄清楚。我通过创建一个只有弱引用属性的简单类来解决这个问题。
更美妙的是,通过使用 NSProxy,我们可以完全将包装对象视为所包含的对象本身!
I am presenting an important limitation of one of the earlier answers, along with an explanation and an improvement.
Johnmph suggested using
[NSValue valueWithNonretainedObject:]
.Note that when you do this, your reference acts not like
__weak
, but rather like__unsafe_unretained
while inside the NSValue object. More specifically, when you try to get your reference back (using [myNSValue nonretainedObjectValue]), your application will crash with an EXC_BAD_ACCESS signal if the object has been deallocated before that time!In other words, the weak reference is not automatically set to nil while inside the
NSValue
object. This took me a bunch of hours to figure out. I have worked around this by creating a simple class with only a weak ref property.More beautifully, by using
NSProxy
, we can treat the wrapper object entirely as if it is the contained object itself!检查 NSValue valueWithNonretainedObject 方法的文档:
Check documentation of NSValue valueWithNonretainedObject method :
我建议不要对抗框架并使用 NSPointerArray 与
NSPointerFunctionsWeakMemory
NSPointerFunctionOption
如下:为我服务很好在场景中,我必须设计一个委托数组,它会自动设置 NULL 的引用。
I'd suggest to not-fight-the-framework and use NSPointerArray with the
NSPointerFunctionsWeakMemory
NSPointerFunctionOption
like this:Served me well in scenarios, where I had to design a delegates array, which auto-NULL's references.
你不想这样做! Cocoa Touch 有几个发送事件的概念,您应该针对每种情况使用正确的概念。
您应该做的是研究如何使用
NSNotificationCenter
类。这是发送具有多个接收者的通知的正确方法。You do not want to do this! Cocoa Touch have several concepts for sending events, you should use the proper concept for each case.
What you should do is to look into how to use
NSNotificationCenter
class. This is the proper way to send a notification that have more than one receiver.NIMBUS 的这个会更简单:
This one from NIMBUS would be more simple:
关键字:
NSHashTable
,在文档中搜索。Keyword:
NSHashTable
, search in documentations.我从 Three20 项目中找到了一些关于这个主题的代码,我希望这会有所帮助......
I found some pieces of code from Three20 project about this topic, i hope this helps...
我找到了一个名为 XMPPFramewrok 的开源库,
项目中有一个多播委托解决方案
https://github.com/robbiehanson/XMPPFramework/wiki/MulticastDelegate
I found an open source library named XMPPFramewrok
There is a multicast delegate solution in the project
https://github.com/robbiehanson/XMPPFramework/wiki/MulticastDelegate
存储在数组或字典中怎么样
What about storing in the array or dictionary