IOS ARC NSMutableArray do I need to removeAllObjects before alloc new memory for it

匿名 (未验证) 提交于 2019-12-03 01:44:01

问题:

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 }


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