removeAllObjects 和 NSMutableArray 的发布都具有相同的功能吗? [英] Do removeAllObjects and release of an NSMutableArray both have the same functionality?

查看:56
本文介绍了removeAllObjects 和 NSMutableArray 的发布都具有相同的功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码行:

NSMutableArray *array=[[NSMutableArray alloc]init];

这会分配一些内存.我的问题是,我们以后如何释放这块内存,是使用removeAllObjects方法还是[数组释放]?

This allocates some memory. My question is, how can we later release this memory, whether using removeAllObjects method or [array release]?

这两种方法是否具有相同的功能?

Do both methods have the same functionality?

推荐答案

当你向数组中添加一个对象时,该对象的保留计数将增加 1.当你从数组中删除该对象时,保留计数将减少 1来平衡它.但是如果你释放这个数组,所有的对象都会自动收到一个释放消息.所以你不需要在释放数组之前调用removeAllObjects.

When you add an object to the array, the object's retain count will increase by 1. When you remove that object from the array, retain count will decrease by 1 to balance it out. But if you release the array, all objects will automatically receive a release message. So you don't need to call removeAllObjects before releasing the array.

从技术上讲,这两种方法并不相同.如果调用 removeAllObjects,数组将变为空,所有对象都会收到释放消息,但数组本身仍未释放.当你对它调用 release 时,该数组将被释放.

Technically, these two method are not same. If you call removeAllObjects, the array will become empty and all objects will receive a release message but the array itself is still not released. The array will be released when you call release on it.

这篇关于removeAllObjects 和 NSMutableArray 的发布都具有相同的功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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