9

Is there a way to create two dimensional NSArray without nesting arrays in the primitive format aFloatArray[][].

Thank you.

1
  • 3
    What is it that you need to accomplish? Perhaps there may be other approaches even better suited to the problem you're solving. Commented Apr 7, 2010 at 23:51

3 Answers 3

17

Unfortunately not. To create a multi-dimensional NSArray:

NSArray *multiArray = [NSArray arrayWithObjects:
    [NSMutableArray array],
    [NSMutableArray array],
    [NSMutableArray array],
    [NSMutableArray array], nil];

// Add a value
[[multiArray objectAtIndex:1] addObject:@"foo"];

// get the value
NSString *value = [[multiArray objectAtIndex:1] objectAtIndex:0];

However, you can use C code in Objective-C (since it is a strict superset of C), if it fits your need you could declare the array as you had suggested.

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

6 Comments

If you're not using garbage collection, then you're leaking each of the arrays inside multiArray. To fix, use [NSMutableArray array] instead.
Really? It was my understanding that [NSMutableArray array] returned an autoreleased NSMutableArray, which would in turn be retained by the NSArray to which it is being added to! I will definatly look that up because if you are correct, I have a lot of code to go through lol
Yes. The point is that the autoreleased object is retained by the array it is added to. When the parent array is released, so are its contents. If you add [NSMutableArray new] to an array, its retain-count becomes 2. When the parent array is released, each of its children still has a retain-count of 1.
Try to avoid thinking in terms of the retain count. This can often be misleading. Think only in terms of ownership of objects. Since you obtained each of the four arrays with new, you are responsible for releasing them. You don't do that. Possible solutions: change [NSMutableArray new] to [[NSMutableArray new] autorelease] or [NSMutableArray array] or use garbage collection (not an option on iPhone).
Oops, I realize now that I misread Dave's comment. In any case, thanks for the answers :) I was confused because I wasn't aware that NSObject had a 'new' class method and I assumed it was similar to NSArray's 'array' method.
|
12

You can do this:

NSArray *array = @[@[@"0:0", @"0:1"],
                   @[@"1:0", @"1:1"]];

NSString *value = array[1][0];

i think this is much shorter than "objectAtIndex" stuff.

but beware you have use Apple LLVM Compiler version >= 4.0

1 Comment

This is definitely the modern approach.
0

To insert an object in Multidimensional array in Collection or TableView cellForRowAtIndexPath:

NSString *sectionRow = [NSString stringWithFormat:@"%d:%d", indexPath.section, indexPath.row];                
[dictionary setValue:[UIImage imageWithData:imageData] forKey:sectionRow];

To retrieve an object from Multidimensional array in Collection or TableView cellForRowAtIndexPath:

NSString *sectionRow = [NSString stringWithFormat:@"%d:%d", indexPath.section, indexPath.row];    
UIImage *cellImage = [dictionary valueForKey:sectionRow];

Comments

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.