Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to empty properly an NSMutableArray

I am trying to empty an NSMutableArray with the [myarray removeallobjects]; but I am getting error exc_bad_access. Is this the right way to empty the array? I tried to set it to nil but its not working either. Actually what I am doing is filling the array with data and the user has the option to "refresh" the data and I want to empty the array before enter the refreshed data. I cant post any code because is too big.

like image 229
BlackM Avatar asked Jun 19 '11 00:06

BlackM


2 Answers

-[NSMutableArray removeAllObjects] is the correct way of emptying an NSMutableArray. You're most likely getting a crash because you're still using the objects that you removed somewhere, in your UI perhaps.

like image 125
puzzle Avatar answered Oct 11 '22 11:10

puzzle


I think the problem is in this snippet of code that you've provided in the comments,

[checkinArray addObject:checkinsA]; 
[checkinsA.taggedID release];
[checkinsA.taggedName release]; 
[checkinsA release]; 

taggedID and taggedName are properties of the checkinsA object. They should be released in the dealloc method only. The array does not retain the object tree. It retains the root object only. So there shouldn't be a release here. So knock out the two lines in the middle and make it

[checkinArray addObject:checkinsA]; 
[checkinsA release]; 
like image 21
Deepak Danduprolu Avatar answered Oct 11 '22 11:10

Deepak Danduprolu