views:

44

answers:

3

Hi Guys, in my class i have a NSMutableArray declared and used. One of the methods of this class however needs to clear the entire array and add new set of objects into it. What would be the best approach to clear it out and add new objects to it from scratch?

regards peter

A: 

You could release it and allocate a new one. Note: this will cause every object in the array to be sent a release message as well, so you would need to be retaining them elsewhere.

gerry3
+5  A: 

Call

[mArray removeAllObjects];

Then add your objects:

  • Manually
  • In an array
  • By iterating over another data structure

Added this link for more info: http://www.techotopia.com/index.php/Working_with_Objective-C_Array_Objects

Mr-sk
A: 

Assuming you have your new contents in an array, you want:

[myArray setArray:myNewContentsArray];

This will replace the contents of myArray with the contents of myNewContentsArray, leaving the latter untouched.

David Kanarek