1

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];

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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"]

2 Comments

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??
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.