仅当保留计数达到0时,才如何从NSMutableArray中删除对象? [英] How to remove an object from NSMutableArray only when retain count reaches 0?

查看:94
本文介绍了仅当保留计数达到0时,才如何从NSMutableArray中删除对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我不应该检查或使用keepCount,但是我想知道是否有办法仅在其保留计数为0后才将对象从NSMutableArray中移除.

I know I'm not supposed to check or use retainCount, but I'm trying to wonder if there's a way to have an object be removed from an NSMutableArray only after its retain count is 0.

基本上,我想将对象添加到数组,并使这些对象在其他窗口之间共享.当窗口使用它时,我希望保留计数增加1.当它不再使用时,我希望它减少计数.但是,如果某个窗口仍在使用它,那么我希望它可用于所有其他窗口.当所有窗口都不再使用它时,我希望将其从数组中删除并完全释放.

Basically, I want to add objects to an array, and have those objects be shared among other windows. When a window uses it, I want the retain count to go up by 1. When it's no longer used, I want it to go down. But, if some window is still using it, then I want it to be available to all other windows. When all windows are no longer using it, then I want it to be removed form the array and released entirely.

谢谢!

推荐答案

对于发布时自动从数组中删除,您可以使用关联对象,如Dave DeLong在此处所述:
如何向其中添加活动对象NSMutableArray并在发布时将其删除?

For the automatic removal from array on release you could use associated objects, as Dave DeLong described here:
How to add alive object to NSMutableArray and remove them when they're released?

但是使用

But you'd probably be better off using an NSCountedSet, as it implements exactly what you're after. It just lacks item order. To make up with the lack of item order you could use an additional NSMutableArray to keep order and add/remove items to/from it in sync with your counted set.

这篇关于仅当保留计数达到0时,才如何从NSMutableArray中删除对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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