NSMutableArray pointer question

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

  1. macrumors newbie

    Joined:
    Jun 8, 2008
    #1
    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
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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