CSV解析

NSString *path = [[NSBundle mainBundle] pathForResource:
                      @"type" ofType:@"csv"];
    NSString* fileContents =
    [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding error:nil];
// first, separate by new line
NSArray* allLinedStrings =
[fileContents componentsSeparatedByString:@"\r\n"];

// then break down even further
_poiTypes = [[NSMutableDictionary alloc] initWithCapacity:[allLinedStrings count]];
for (int i = 0; i NSString* strsInOneLine =
[allLinedStrings objectAtIndex:i];
NSString *trimmedString = [strsInOneLine stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSArray* singleStrs =
[trimmedString componentsSeparatedByString:@","];
NSString *strType = [singleStrs objectAtIndex:0];
NSString *strValue = [singleStrs objectAtIndex:1];
NSString *strValue1 = [strValue substringToIndex:[strValue length]-2];
[_poiTypes setObject:strType forKey:strValue1];

}

}
posted @ 2015-07-26 16:06  小毛驴  阅读(365)  评论(0编辑  收藏  举报