问题
Is there a way to create two dimensional NSArray without nesting arrays in the primitive format aFloatArray[][].
Thank you.
回答1:
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.
回答2:
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
回答3:
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];
来源:https://stackoverflow.com/questions/2596593/multi-dimensional-nsarray-object