Difference of [nsmutablearray removeAllObjects] and [nsmutablearray release] [duplicate]
Possible Duplicate:
Do r开发者_JAVA技巧emoveAllObjects and release of an NSMutableArray both have the same functionality?
The difference of [nsmutablearray removeAllObjects]
and [nsmutablearray release]
?
Waiting for your help.
First off, both the methods you're asking about are instance methods, not class methods. i.e., they can only be called on instances of the NSMutableArray class--
NSMutableArray* i = [NSMutableArray arrayWithCapacity:1];
[i removeAllObjects];
or,
NSMutableArray* j = [[NSMutableArray alloc] initWithCapacity:1];
[j release];
removeAllObjects
, well, empties the array, and sends a release
message to each object removed from the array. However, the array object itself still exists and you could add items to it in the future by executing [i addObject:obj]
where obj is a valid object.
release
on the other hand, relinquishes control of the array object, and decrements its retain count by 1. When the retain count of an object hits 0, it'll be deallocated from memory. Deallocation of the array object would cause a release
message being sent to each of the objects stored in the array.
The arrayWithCapacity
method creates i
as an autoreleased object, and you don't call release
on it. If you do so and this causes the object to be dealloc
-ed, it would cause an exception later on when the NSAutoReleasePool
object sends it a release
message.
精彩评论