开发者

Objective C read csv file and use array

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?


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
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜