Dereference (release?) NSMutableArray
in my class i have a NSMutableArray declared and used. One of the methods 开发者_Python百科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
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
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.
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.
精彩评论