removeObject是否在NSMutableArray对象中释放对象? [英] Does removeObject release the object in an NSMutableArray of objects?

查看:135
本文介绍了removeObject是否在NSMutableArray对象中释放对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么时候使用removeObject在数组中删除对象,如果删除的对象正确处理。

I was wondering when you remove an object using removeObject in an array if that removed object is handled properly. Would the object being removed be released?

推荐答案

NSMutableArray会释放它。如果这是它的最后一个retain,它将被释放。从文档:

The NSMutableArray will release it. If that's the last retain on it, it will be deallocated. From the documentation:


像NSArray一样,NSMutableArray的实例保持对它们内容的强引用。如果您不使用垃圾回收 [ Jed: iPhone不会] ,当您将对象添加到数组时,对象会收到一个保留消息。当一个对象从一个可变数组中删除时,它接收一个释放消息。如果没有对对象的进一步引用,这意味着对象被释放。如果你的程序保持对这样一个对象的引用,引用将变得无效,除非你在从数组中删除之前发送一个保留消息。

Like NSArray, instances of NSMutableArray maintain strong references to their contents. If you do not use garbage collection [Jed: the iPhone does not], when you add an object to an array, the object receives a retain message. When an object is removed from a mutable array, it receives a release message. If there are no further references to the object, this means that the object is deallocated. If your program keeps a reference to such an object, the reference will become invalid unless you send the object a retain message before it’s removed from the array.

请参见 NSMutableArray文档。它们的例子实际上指的是 removeObjectAtIndex:

See the NSMutableArray documentation. Their example, in fact, refers to removeObjectAtIndex::

id anObject = [[anArray objectAtIndex:0] retain];
[anArray removeObjectAtIndex:0];
[anObject someMessage];

这篇关于removeObject是否在NSMutableArray对象中释放对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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