Do removeAllObjects and release of an NSMutableArr

2019-03-03 22:17发布

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条回答
一纸荒年 Trace。
2楼-- · 2019-03-03 23:08

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.

查看更多
登录 后发表回答