CSV格式文件解析

来源:互联网 发布:qq群监控软件 编辑:程序博客网 时间:2024/04/30 07:08

+ (NSArray *)arrayWithContentOfSimpleCsvFile:(NSString *)path {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    

    NSString * content = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

    NSArray * rows = [content componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

    [content release];

    NSUInteger rowsCount = rows.count;

    NSMutableArray * result = [[NSMutableArray alloc] initWithCapacity:rowsCount];

    for (NSInteger idx = 0; idx < rowsCount; ++idx) {

        NSString * row = [rows objectAtIndex:idx];

        if ([row length] > 0) {

            NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

            NSArray * columns = [row componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];

            [result addObject:columns];

            [pool drain];

        }

    }

    [pool drain];

    [result autorelease];

    return result;

}