Objective C读取csv文件并使用数组
我有一个 csv 文件,如下所示:
1#one#two#three#four;
2#apple#tower#flower#robot;
我用以下代码读取此文件:
NSString *resourceFileName = @"PrenotazioniDb";
NSString *pathToFile =[[NSBundle mainBundle] pathForResource: resourceFileName ofType: @"txt"];
NSError *error;
NSString *fileString = [NSString stringWithContentsOfFile:pathToFile encoding:NSUTF8StringEncoding error:&error];
if (!fileString) {
NSLog(@"Error reading file.");
}
NSScanner *scanner = [NSScanner scannerWithString:fileString];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"\n#; "]];
NSString *one = nil, *two = nil, *three = nil, *four = nil;
while ([scanner scanUpToString:@"#" intoString:&one] && [scanner scanUpToString:@"#" intoString:&two] && [scanner scanUpToString:@"#" intoString:&three] && [scanner scanUpToString:@"#" intoString:&four]{
}
但我想将文件存储在数组中,我该怎么办?我应该使用两个数组:一个用于行,一个用于单个单词;例如,在第一个数组中我存储的位置中
1#one#two#three#four;
,在第二个数组中我存储在第一个位置“1”中的第二个“一”中的第三个“二”分机中......
我能做什么?
I have a csv file as this:
1#one#two#three#four;
2#apple#tower#flower#robot;
I read this file with this code:
NSString *resourceFileName = @"PrenotazioniDb";
NSString *pathToFile =[[NSBundle mainBundle] pathForResource: resourceFileName ofType: @"txt"];
NSError *error;
NSString *fileString = [NSString stringWithContentsOfFile:pathToFile encoding:NSUTF8StringEncoding error:&error];
if (!fileString) {
NSLog(@"Error reading file.");
}
NSScanner *scanner = [NSScanner scannerWithString:fileString];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"\n#; "]];
NSString *one = nil, *two = nil, *three = nil, *four = nil;
while ([scanner scanUpToString:@"#" intoString:&one] && [scanner scanUpToString:@"#" intoString:&two] && [scanner scanUpToString:@"#" intoString:&three] && [scanner scanUpToString:@"#" intoString:&four]{
}
but I want memorize the file in an array, What can I do? I should use two array: one for line and one for single word; for example in first array in a position I store
1#one#two#three#four;
and in the second array I store in first position "1" in second "one" in third "two" ext....
What Can i Do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 componentsSeparatedByString 方法。
印刷:
You can use the componentsSeparatedByString method.
prints: