tags:

views:

40

answers:

4

Is there any practical difference between the following two code snippets:

NSObject * obj = [[_mutableArrayOne objectAtIndex:i] retain];
[_mutableArrayOne removeObject:obj];
[_mutableArrayTwo addObject:obj];
[obj release];

and

NSObject * obj = [_mutableArrayOne objectAtIndex:i];
[_mutableArrayTwo addObject:obj];
[_mutableArrayOne removeObject:obj];
+1  A: 

Only the extra retain/release cycle that you ask it to do.

It might have some performance impact inside a loop of time-sensitive processing?

ohhorob
Thanks. That's pretty much what I thought.
Steve
removeObject remove all occurrences - in second case you will not have the 'objec' in the mutable array any more .... see my answer...
Girish Kolari
This is blatantly not true. In the second case, there is a small window of time when the object is in *both* arrays. In a multithreaded environment, this may be a problem (conversely, having the object in no arrays might also be a problem).
JeremyP
+1  A: 

Other than the need to call retain and release twice instead of once, no. The end result is the same.

Marcelo Cantos
+1  A: 

If you want to own the object in the index 'i' then remove object from array and then you can add it back to the array in the later stage of the project.

I suggest option 2 should be careful : the object will not be in the array any more.

The result is different -- Option 1 you will have the 'object' in mutable array and in option 2 'object' will not be in array (removeObject remove all occurrences)

Girish Kolari
Not sure if I'm understanding you properly. Could you elaborate?
Steve
removeObject remove all occurrencesin your second case you add the occurrence of the 'obj' first then there will be duplicate entry in the array then you ask to remove the 'obj' occurrence using 'removeObject' which will remove all occurrence(apple apple documentation) of the 'obj' in the array so no more 'obj' is not present in the arraybut in 1 case obj will be present in the end of the array.
Girish Kolari
sorry i missed some detail actually both are same
Girish Kolari
+1  A: 

Both are same with respect to Memory Management and programming logic. Only in the first case you have to operate a pair of extra retain/release operation.

Sadat