如何通过从文件读取数据来正确创建数组的数组
到目前为止我有:
//read in data from CSV file and separate it into 'row' strings:
// where dataString is simply a CSV file with lines of CSV data
// 31 lines with 9 integers in each line
NSArray *containerArray = [dataString componentsSeparatedByString:@"\n"];
NSArray *rowTemp; //local variable just for my sake
NSMutableArray *tableArray;//mutable array to hold the row arrays
//For each index of containerArray:
//take the string object (string of CSV data) and then,
//create an array of strings to be added into the final tableArray
for (int i = 0; i < [containerArray count]; i++) {
rowTemp = [[containerArray objectAtIndex:i] componentsSeparatedByString:@","];
[tableArray addObject:rowTemp];
}
然后当我尝试以下操作时,它返回: (null)
NSLog(@"Row 6, cell 1 is %@", [[tableArray objectAtIndex:5] objectAtIndex:0]);
有什么想法吗?有更好的办法吗?
仅供参考,该数据是静态的,不太可能改变。任何创建和填充静态数组而不是使用可变数组的方法都将不胜感激。
提前致谢。
So far I have:
//read in data from CSV file and separate it into 'row' strings:
// where dataString is simply a CSV file with lines of CSV data
// 31 lines with 9 integers in each line
NSArray *containerArray = [dataString componentsSeparatedByString:@"\n"];
NSArray *rowTemp; //local variable just for my sake
NSMutableArray *tableArray;//mutable array to hold the row arrays
//For each index of containerArray:
//take the string object (string of CSV data) and then,
//create an array of strings to be added into the final tableArray
for (int i = 0; i < [containerArray count]; i++) {
rowTemp = [[containerArray objectAtIndex:i] componentsSeparatedByString:@","];
[tableArray addObject:rowTemp];
}
Then when I try the following, it returns: (null)
NSLog(@"Row 6, cell 1 is %@", [[tableArray objectAtIndex:5] objectAtIndex:0]);
Any ideas? is there a better way?
FYI this data is static and very unlikely to change. Any way to create and populate a static array rather than using a mutable array, would be much appreciated.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚得到答案!在 iPhoneSDK 论坛的一些帮助下,我实际上未能创建 tableArray。我在上面的代码中所做的只是创建变量。 NSMutableArray *tableArray;并且实际上并没有创建我想要的可变数组。相反,我应该做的是: NSMutableArray *tableArray = [NSMutableArray arrayWithCapacity: [containerArray count]];这就像一个魅力。
I just got the answer! with some help from iPhoneSDK forums, and what I failed to do was actually create the tableArray. What I did in the code above was merely create the variable. NSMutableArray *tableArray; and did not actually create the mutable array I wanted. Instead what I should have done was: NSMutableArray *tableArray = [NSMutableArray arrayWithCapacity: [containerArray count]]; Which worked like a charm.