iOS 2D Array

Nnavick

macrumors regular
Original poster
Oct 13, 2010
100
0
Hi,
I created 2D array like this:

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

thanks!
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
8,671
4,943
vertical
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.
 
Comment

Nnavick

macrumors regular
Original poster
Oct 13, 2010
100
0
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.
Thanks!!!
Did it step by step and it was really simple.
 
Last edited:
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.