0

I'm traversing through a two-dimensional array like this:

for (menuViewController *aSelection in mainDataArray) {
    ...
}

However, how do I access the lower arrays' data? This following code doesn't work, but gives you the idea of what I mean:

for (menuViewController *aSelection in mainDataArray) {
    NSLog(@"Data: %@", [aSelection objectAtIndex:2]);
}
jscs
  • 63,694
  • 13
  • 151
  • 195
Custom Bonbons
  • 1,609
  • 2
  • 12
  • 17
  • 1
    In what way doesn't it work? If `aSelection` is indeed an array, then that should do the trick. If it isn't (and it's not clear why you're typing it as something called `menuViewController` if it is), then why do you say you have a 2-D array? – jscs Jul 08 '12 at 20:58
  • Are you sure menuViewController is an array and not a viewController??? – user1447414 Jul 08 '12 at 21:01
  • Yeah I can confirm that mainDataArray defo is an array. And it lists the array in NSlog. menuViewController is referring to my view controller, but maybe I have made an error here. What SHOULD that refer to??? It confused me to be honest. – Custom Bonbons Jul 08 '12 at 21:08

2 Answers2

1

If you are traversing an array within an array, can you try the following?

for (NSArray* aSelection in mainDataArray) {
    NSLog(@"Data: %@", [aSelection objectAtIndex:2]);
}
Just a coder
  • 15,480
  • 16
  • 85
  • 138
  • Thank you user76859403, it was the NSArray syntax that I had got wrong (putting my viewController name there instead). Although it did work for one-dimensional array traversing! – Custom Bonbons Jul 08 '12 at 21:17
  • 1
    I'd advise you to put the **type** that you are traversing. My guess is that **menuViewController** is not of type NSArray. But i can't see your code on how you used it the first time :) – Just a coder Jul 08 '12 at 21:24
1

Why don't you do a second for-in statement

for (NSArray* aSelection in mainDataArray) {
    for (NSArray* bSelection in aSelection) {
        //Do something innovative :)
    }
}
esreli
  • 4,993
  • 2
  • 26
  • 40