I have similar question to this: What is the difference between setting object = nil and [object release] VS [object release] and object = nil?
NSMutableArray *myExampleArray = [[NSMutableArray alloc] init];
myExampleArray = nil;
I use iOS 5.0 automatic reference counting, so in fact I don’t release any objects. So if I assign it to nil is it equal to [myExampleArray release] ?
I know that i can’t later use myExampleArray without re-initial it. So next question. What is the best way to clear this NSArray?
Yes, in an ARC environment you never call release. So assigning nil to the variable will release the object.
In a non ARC environment, you would do a release on your own, so the object gets destroyed. But the variable would still point to the old object adress. But there is no object anymore, so you would probably get a crash (
EXC_BAD_ACCESS), if you use the variable later. If you also assign nil to it, that won’t happen. Because the variable won’t point to the old object address anymore.Your other question:
If you need the array later again, you can call
removeAllobjectson a NSMutableArray to remove all added objects, like Ankit Gupta said already. This will result in an empty array, that is still alive, so you can reuse it.