NSScanner 和 CSV 文件
我有一个包含四个字段的 CSV 文件:“Woonplaats”、“Gemeente”、“Provincie”、“Latitude”和“Longitude”。
示例值:
格拉夫兰,韦德梅伦,北荷兰省,52.24412000,5.12150000
使用下面的代码,我在文本中获取字符串,然后我想将其保存在数组中。我应该如何使用 NSScanner 从此字符串获取数据并将其保存在包含字典的数组中?
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cities" ofType:@"csv"];
NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil ];
NSScanner *scanner = [NSScanner scannerWithString:myText];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"\n ,"]];
NSMutableArray *newPoints = [NSMutableArray array];
I have a CSV file with four fields, "Woonplaats", "Gemeente", "Provincie", "Latitude", and "Longitude".
Example values:
Graveland,Wijdemeren,Noord-Holland,52.24412000,5.12150000
Using the code below, I get the string in my text, and then I want to save it in array. How should I use NSScanner
to get data from this string and save in an array containing dictionaries?
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cities" ofType:@"csv"];
NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil ];
NSScanner *scanner = [NSScanner scannerWithString:myText];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"\n ,"]];
NSMutableArray *newPoints = [NSMutableArray array];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信这就是您正在寻找的。我使用 Dan Wood 论坛帖子和根据您的需要修改它。
输出是:
I believe this is what you're looking for. I used Dan Wood forum post and modified it for your need.
And the output is: