Array Garbage Collection and Indexing

Discussion in 'iOS Programming' started by KardelSharpeye, May 14, 2009.

  1. KardelSharpeye macrumors member

    KardelSharpeye

    Joined:
    Apr 28, 2009
    #1
    Good morning,

    I was just wondering if i have an array in header file declared as:
    NSArray *myArray;
    NSMutableArray *myMuArray;

    and in my implementation file i have:
    myArray = [NSArray arrayWithObjects:eek:bj1, obj2, nil];

    myMuArray = [[NSMutableArray alloc] init];
    [myMuArray addObject:eek:bj1];
    [myMuArray addObject:eek:bj2];

    Question 1:
    what is the proper way to dispose these allocated memory?
    [myArray release] for the array and...
    [myMuArray removeAllObjects] ???

    Question 2:
    It seems like i can only remove and add items in array but is there anyway i can reassign the value of an item in an array?
    something like myArray = 20 or myMuArray = 30??

    Thanks in advance,
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Releasing an array automatically removes and releases all of its objects, so release it when you're done with the entire array, and removeAllObjects when you want to keep the array but empty it.

    There is no garbage collector on the iPhone. myArray is autoreleased and doesn't need a call to release, but myMuArray does.



    Use the replaceObjectAtIndex:withObject: method.
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3

    First, I suggest putting your code in [ CODE ] tags to make it easier for us to read (and avoid the smilies). Use the # icon in the toolbar.

    Question 1: Since you alloc'd your myMuArray, just do a [myMuArray release].

    Question 2: myArray is not mutable, so you can't even remove or add items. But for NSMutableArray, in the class reference you'll find a number of replace... methods.
     
  4. KardelSharpeye thread starter macrumors member

    KardelSharpeye

    Joined:
    Apr 28, 2009
    #4
    cool thanks guys!:) eveything works! no more :(
     

Share This Page