如何通过从文件读取数据来正确创建数组的数组

发布于 2024-12-22 18:13:52 字数 990 浏览 4 评论 0原文

到目前为止我有:

//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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来世叙缘 2024-12-29 18:13:52

我刚刚得到答案!在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文