【发布时间】:2010-04-07 23:28:01
【问题描述】:
有没有一种方法可以创建二维 NSArray 而无需以原始格式 aFloatArray[][] 嵌套数组。
谢谢。
【问题讨论】:
-
你需要完成什么?也许还有其他方法更适合您正在解决的问题。
标签: objective-c multidimensional-array nsarray
有没有一种方法可以创建二维 NSArray 而无需以原始格式 aFloatArray[][] 嵌套数组。
谢谢。
【问题讨论】:
标签: objective-c multidimensional-array nsarray
很遗憾没有。创建多维 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];
但是,您可以在 Objective-C 中使用 C 代码(因为它是 C 的严格超集),如果它符合您的需要,您可以按照您的建议声明数组。
【讨论】:
multiArray 中的每个数组。要修复,请改用[NSMutableArray array]。
[NSMutableArray new] 添加到数组中,则其保留计数变为2。当父数组被释放时,其每个子数组的保留计数仍为1。
你可以这样做:
NSArray *array = @[@[@"0:0", @"0:1"],
@[@"1:0", @"1:1"]];
NSString *value = array[1][0];
我认为这比“objectAtIndex”的东西要短得多。
但请注意,您使用的是 Apple LLVM 编译器版本 >= 4.0
【讨论】:
在 Collection 或 TableView cellForRowAtIndexPath 中的多维数组中插入对象:
NSString *sectionRow = [NSString stringWithFormat:@"%d:%d", indexPath.section, indexPath.row];
[dictionary setValue:[UIImage imageWithData:imageData] forKey:sectionRow];
从 Collection 或 TableView cellForRowAtIndexPath 中的多维数组中检索对象:
NSString *sectionRow = [NSString stringWithFormat:@"%d:%d", indexPath.section, indexPath.row];
UIImage *cellImage = [dictionary valueForKey:sectionRow];
【讨论】: