【OC】 将CSV文件加载到字典(NSMutableDictionary)

下面的代码用来从CSV读取文件并存储到字典中:


-(id)addCsvFileToDictionary
{
    NSString *filepath = [[NSBundle mainBundle]pathForResource:@"Test" ofType:@"csv"];
    NSData   *data = [NSData dataWithContentsOfFile:filepath];
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSArray  *array = [string componentsSeparatedByString:@"\r\n"];
    NSString *headerStr = array.firstObject;
    NSString *flag = @",";
    NSArray  *headerArray = [headerStr componentsSeparatedByString:flag];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    for (int i=1; i0 && value.length) {
                dict[key] = value;
            }
        }
    }
    return dict;
}

你可能感兴趣的:(Objective-C,MacOS开发,objective-c,macos)