Multi-dimensional NSArray object

て烟熏妆下的殇ゞ 提交于 2019-11-27 06:48:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!