IOS ARC NSMutableArray do I need to removeAllObjec

2019-09-02 01:55发布

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.

2条回答
看我几分像从前
2楼-- · 2019-09-02 02:36

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
}
查看更多
乱世女痞
3楼-- · 2019-09-02 02:41

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.

查看更多
登录 后发表回答