PDA

View Full Version : NSMutableArray pointer question




bkphat
Aug 1, 2008, 03:22 PM
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
Aug 1, 2008, 10:39 PM
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.