views:

261

answers:

2

Hi,

I have a mutable array and i would like to arrange it in a nested order upon some criteria. To achieve this I'd like to move certain elements to mutable arrays of another elements, removing them from the main mutable array, but without releasing them. Actually the question is about removing elements from array without releasing them How do i achieve it?

Thanks

+5  A: 

You cannot remove an object from an array without the array releasing it. If you want to make sure it sticks around, just retain it yourself first, and release it when you're done. These are pretty cheap operations, so you shouldn't worry too much about it.

Ben Gottlieb
upvote your answer too, thanks :)
Nava Carmon
+3  A: 

Since you're moving items from one array to another, it would be easier if you first added the object to the new array and then removed it from the original array.

  1. When you add it to the new array it is implicitly retained.
  2. When you remove it from the old array it is implicitly released.

This is faster than retaining it, removing it from the array, adding it to the new array and then releasing it.

Abizern
that's exactly what i did, but my problem was apparently that one of the members of my object was an NSString, which was initiated as [NSString stringWithString] and it got lost some time after moving objects around. I changed it with NSMutableString and the code has started working.
Nava Carmon
Nava Carmon: That didn't help: The mutability of an object has nothing to do with its lifetime. You need to check your ownerships—it sounds like you are failing to retain or copy the string at some point.
Peter Hosey