Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bkphat

macrumors newbie
Original poster
Jun 8, 2008
17
0
Hello,

Let's say I have an NSMutable array filled with instances of MyObject

I now want to extract a particular MyObject, edit it and pass it along to another call:

MyObject *x = [[MyObject alloc] init];
x = [myArray objectAtIndex:i];
/*do something to x...*/

But, the next time I acess myArray at the same index, it gives me the value of the edited x. I guess x is a pointer, that when edited changes the value of the array also. How do I not point to the array object, but rather, when assignign x, only get the value of the array object (not a pointer). I want, in some sense, a copy of the value of the array object, and not a pointer.

thanks
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
You would need to make MyObject conform to the NSCopying protocol, and implement the copyWithZone: method. Then, you'd call [[[myArray objectAtIndex:i] copy] autorelease] to get a copy of the object.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.