Changing content of object inside NSMutableArray

  • HI

    I have saved some objects inside GameArray (type NSMutableArray) and I
    would like to change of content of an object inside that array.

    GameObj is type of UIView so it has a frame.

    GameObj* p = [GameArray objectAtIndex:0];
    p.frame = frame;

    This works, but this is not changing the content inside the GameArray.

    How this can be done.

    Any help will be very much appreciated.

    Best regards
    -Agha
  • On Jul 2, 2009, at 4:36 PM, Agha Khan wrote:

    > This works, but this is not changing the content inside the GameArray.
    >
    > How this can be done.

    Use -replaceObjectAtIndex:withObject:. You are responsible for
    maintaining your own arrays; changing a pointer will not change the
    contents of the array.

    Nick Zitzmann
    <http://www.chronosnet.com/>
  • On Jul 2, 2009, at 5:36 PM, Agha Khan wrote:

    > I have saved some objects inside GameArray (type NSMutableArray) and
    > I would like to change of content of an object inside that array.
    >
    > GameObj is type of UIView so it has a frame.
    >
    > GameObj* p = [GameArray objectAtIndex:0];
    > p.frame = frame;
    >
    > This works, but this is not changing the content inside the GameArray.

    What do you mean it "is not changing the content inside the
    GameArray"?  By what means are you detecting this?

    It is most definitely the case that the above code snippet is changing
    the frame of the object at index 0 of the array.  An array contains
    references to objects.  It doesn't really contain the objects
    themselves.  So, the pointer 'p' in your snippet is a reference to the
    same object that the array has a reference to.  There's no way for the
    change you make through 'p' to not affect the object referred to by
    the array, since they are the same object.

    If you think it is not changing the object in the array, then you are
    confused or mistaken.

    Regards,
    Ken