问题
NSMutableArray * arrayTest;
-(void) setContent
{
//must I call [array removeAllObjects]; ?
arrayTest = [[NSMutableArray alloc] init]
[arrayTest addObject:@"str"];
...//add many objects
}
I call this function at different code snippet. do I need to removeAllObjects of arrayTest before , then alloc memory for arrayTest every time ? I use ARC . I don't want my app memory to increase every time I call this function.
回答1:
No, what you have is fine. You don't need to call removeAllObjects
under ARC or non-ARC.
When the old array is deallocated, it will take care of releasing all of the objects in the old array.
回答2:
Check if arrayTest exists before alloc'ing memory. If you don't you'll have a new array every time the method is called (assuming you want to keep the array and it's content around for a while). Or even better.. move the alloc into the init of the class.
-(void) setContent
{
if(!arrayTest){
arrayTest = [[NSMutableArray alloc] init];
}
[arrayTest addObject:@"str"];
...//add many objects
}
来源:https://stackoverflow.com/questions/13476920/ios-arc-nsmutablearray-do-i-need-to-removeallobjects-before-alloc-new-memory-for