Does the removeAllObjects method of an NSArray or NSMutableArray release memory?

六月ゝ 毕业季﹏ 提交于 2019-12-05 18:39:14

An NS[Mutable]Array is a collection of references to object. Calling -removeAllObjects nils those references. The capacity and memory used by the array itself stay the same. In the case of NSMutableArray, those references can be reused to point to other objects. NSArray lacks the method -removeAllObjects because it's not mutable.

removeAllObjects will be marginally slower for a high number of objects than initWithCapacity. The real performance difference would be having to avoid growing the array when the number of objects reaches the limit.

As others pointed out, the objects are only released if there are no more strong references to them.

Yes, it releases all the objects, it doesn't leak. (It doesn't, of course, explicitly deallocate them - they are only deallocated if the only reference to them was held by the array.)

No it does not leak objects. It is the same as removing every object individually except it is more convenient and faster.

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