【问题标题】:Objective C read csv file and use arrayObjective C读取csv文件并使用数组
【发布时间】:2011-04-01 14:00:18
【问题描述】:

我有一个这样的 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;

在第二个数组中,我存储在第三个“two”扩展中的第二个“one”中的第一个位置“1”......

我能做什么?

【问题讨论】:

    标签: objective-c arrays xcode csv


    【解决方案1】:

    您可以使用componentsSeparatedByString 方法。

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

    打印:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-24
      • 1970-01-01
      • 2012-08-19
      • 1970-01-01
      相关资源
      最近更新 更多