Do removeAllObjects and release of an NSMutableArray both have the same functionality?

我是研究僧i 提交于 2019-12-02 21:31:59

问题


I have written the following line of code:

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

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:


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.

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.



来源:https://stackoverflow.com/questions/3164154/do-removeallobjects-and-release-of-an-nsmutablearray-both-have-the-same-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!