Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'd like to initialize an array of arrays using NSMutableArray in Objective-C. Here is my init code, I believe it should work, as it's essentially creating an array of objects, except the objects happen to be arrays themselves.

I am getting an EXEC_BAD_ACCESS.

    NSMutableArray *aoaNumbers = [NSMutableArray arrayWithObjects:
                  [NSArray arrayWithObjects:@"5", @"1", @"4", @"8", nil],
                  [NSArray arrayWithObjects:@"4", @"5", @"8", @"3", nil],
                  [NSArray arrayWithObjects:@"2", @"4", @"2", @"3", nil],
                  [NSArray arrayWithObjects:@"7", @"6", @"2", @"3", nil], nil];

Please help me understand what is wrong with this initialization? I believe if I do [aoaNumbers addObject: tempArray], it will work fine, I didn't try that out but I am curious how is it any different from the above initialization. Thank you.

share|improve this question
4  
There's nothing wrong with that code; your problem is elsewhere. On what line do you actually get the bad access? –  Tommy Dec 17 '12 at 7:26
1  
Your code is 100% correct. But I would like to say, your inside array or 2nd D array is not mutable if you try to add any object in 2ndD array you will be in trouble. Even if you use [aoaNumbers addObject: tempArray], that is fine, but tempArray must be NSMutableArray so that later you can alter it. –  Anoop Vaidya Dec 17 '12 at 7:43
    
Thanks for confirming that the code is good, made me recheck my code closely, I had add a new line in there as follows, string "9" was missing '@' sign before it, yikes! [NSArray arrayWithObjects:@"2", @"11", @"4", "9", nil], –  Madhav Sbss Dec 17 '12 at 11:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.