Mic*_*all 7 csv objective-c nsstring nsarray
我使用以下方法将CSV文件读入数组:
NSString *theWholeTable = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"example" ofType:@"csv"]
encoding:NSUTF8StringEncoding
error:NULL];
NSArray *tableRows = [theWholeTable componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
Run Code Online (Sandbox Code Playgroud)
并且数组中的每个第二个对象都是空的,任何想法为什么?
CSV文件数据如下所示:10,156,326,614,1261,1890,3639,5800,10253,20914 20,107,224,422,867,1299,2501,3986,7047,14374
10和20是每个新线的开始.
提前致谢.
编辑 我尝试使用以下代码:
NSArray *tableRows = [theWholeTable componentsSeparatedByString:@"\n"];
Run Code Online (Sandbox Code Playgroud)
这也是我想要的方式.
虽然我仍然不确定为什么newlineCharacterSet创建了空对象......
如果您的CSV文件来自非UNIX系统,则它可能包含多个行分隔符(例如,\r\n而不是\n).在这种情况下,componentsSeparatedByCharactersInSet将在\r和之间为空字符序列插入空字符串\n.
您可以NSArray使用此方法删除空字符串:
tableRows = [tableRows filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1293 次 |
| 最近记录: |