tags:

views:

21

answers:

2

How do I add objects to a two-dimensional array? The array is initiated with a couple items (see below), but I need to add more items to the array.

      images_ = [[NSArray alloc] initWithObjects:
             [NSArray arrayWithObjects:@"http://farm3.static.flickr.com/2735/4430131154_95212b8e88_o.jpg", @"http://farm3.static.flickr.com/2735/4430131154_17d8a02b8c_s.jpg", nil],
             [NSArray arrayWithObjects:@"http://farm5.static.flickr.com/4001/4439826859_19ba9a6cfa_o.jpg", @"http://farm5.static.flickr.com/4001/4439826859_4215c01a16_s.jpg", nil],nil];
+1  A: 

Simple answer, add more items ;)

I don't know what's the problem?

EDIT:

Oh, I see, you need a NSMutableArray if you want to add objects later on!

brutella
A: 

Something like this:

NSMutableArray *firstSubArray = [[NSMutableArray arrayWithObjects:@"http://farm3.static.flickr.com/2735/4430131154_95212b8e88_o.jpg", @"http://farm3.static.flickr.com/2735/4430131154_17d8a02b8c_s.jpg", nil] retain];

NSMutableArray *secondSubArray = [[NSMutableArray arrayWithObjects:@"http://farm5.static.flickr.com/4001/4439826859_19ba9a6cfa_o.jpg", @"http://farm5.static.flickr.com/4001/4439826859_4215c01a16_s.jpg", nil] retain];

images_ = [[NSArray alloc] initWithObjects: firstSubArray, secondSubArray ,nil];

[secondSubArray addObject: @"New string"]
Artem Tikhomirov
Perfect thanks!
Wally
Question - the [secondSubArray addObject:@"New string"] is just adding one element to the array. Both the firstSubArray and secondSubArray are two-dimensional arrays. How can I add entries to these arrays? Can't seem to do it using the [secondArray addObject:...] code. What am I missing here??
Wally
firstSubArray and secondSubArray are rows in two-dimentional array images_. They are normal arrays themselves. You could edit them as you can. You could add another row later, but you'll need to user NSMutableArray for images_ array two.
Artem Tikhomirov