NSMutableArray pointer question

Discussion in 'iOS Programming' started by bkphat, Aug 1, 2008.

  1. bkphat macrumors newbie

    Jun 8, 2008

    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.

  2. kainjow Moderator emeritus


    Jun 15, 2000
    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.

Share This Page