2D Array

Discussion in 'iOS Programming' started by Nnavick, May 26, 2012.

  1. Nnavick, May 26, 2012
    Last edited: May 26, 2012

    Nnavick macrumors regular

    Joined:
    Oct 13, 2010
    #1
    Hi,
    I created 2D array like this:

    PHP:
      twoLines = [[NSMutableArray allocinitWithCapacity2];
        [
    twoLines insertObject: [NSArray arrayWithObjects: @"LS1.png", @"LS2.png", @"LS3.png", @"LS4.png",
                              @
    "LS5.png", @"LS6.png" , @"LS7.png", @"LS8.png", @"LS9.png", @"LS10.png",nilatIndex0];
        [
    twoLines insertObject: [NSArray arrayWithObjects:  @"LS11.png", @"LS12.png", @"LS13.png", @"LS14.png", @"LS15.png", @"LS16.png" , @"LS17.png", @"LS18.png", @"LS19.png", @"LS20.png"nilatIndex1];
        
    how do I replace object at index X,Y?

    thanks!
     
  2. lloyddean, May 26, 2012
    Last edited: May 26, 2012

    lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
  3. Nnavick thread starter macrumors regular

    Joined:
    Oct 13, 2010
    #3
    What?
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    You can't replace an object at X,Y with the arrays you have.

    The internal arrays are NSArray, which means they're immutable, which means you can't replace objects in them.

    Use NSMutableArray as your internal arrays. Then the solution should be obvious:
    1. Get the internal array at index X (this should be obvious).
    2. Store it in a temporary variable (again, should be obvious).
    3. Using the temporary variable, replace the object at index Y (also obvious).

    If you break the problem down by thinking it through step by step, a solution will often become obvious. Problems may also become obvious, such as if the internal array (steps 2 & 3) isn't NSMutableArray, then you won't be able to replace an object within it.

    It should also be semi-obvious that if you're going to be using a 2D array a lot, you could make a class that encapsulates the behavior and provides easily used methods that take two indexes, X and Y.
     
  5. Nnavick, May 26, 2012
    Last edited: May 26, 2012

    Nnavick thread starter macrumors regular

    Joined:
    Oct 13, 2010
    #5
    Thanks!!!
    Did it step by step and it was really simple.
     

Share This Page