Objective C读取csv文件并使用数组

发布于 2024-10-29 06:40:32 字数 1145 浏览 2 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(1

余生再见 2024-11-05 06:40:32

您可以使用 componentsSeparatedByString 方法。

NSString *list=@"1#one#two#three#four"; 
NSArray *listItems = [list componentsSeparatedByString:@"#"];
NSLog(@"listItems= %@",listItems);

印刷:

listItems= (
    1,
    one,
    two,
    three,
    four
)

You can use the componentsSeparatedByString method.

NSString *list=@"1#one#two#three#four"; 
NSArray *listItems = [list componentsSeparatedByString:@"#"];
NSLog(@"listItems= %@",listItems);

prints:

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