FatalMojo
FatalMojo

Reputation: 435

Multi-dimensional NSArray object

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

Thank you.

Upvotes: 9

Views: 20633

Answers (3)

Badruduja
Badruduja

Reputation: 31

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

Upvotes: 0

Chakalaka
Chakalaka

Reputation: 2827

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

Upvotes: 12

pixel
pixel

Reputation: 5298

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.

Upvotes: 17

Related Questions