我有一个包含数组和字典的.plist文件。我想将其转换为CSV文件,以便我可以将其在线上传到数据库。有谁知道一个简单的方法吗?
答案 0 :(得分:4)
查看以下代码
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];
NSArray *commentArray = [NSArray arrayWithContentsOfFile:plistPath];
NSMutableString *mainString=[[NSMutableString alloc]initWithString:@""];
for(int i=0;i<[commentArray count];i++ ) {
NSString *string=[[commentArray objectAtIndex:i]objectForKey:@"cmtName"];
string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
[mainString appendFormat:@"\"%@\"",string];
string=[[commentArray objectAtIndex:i]objectForKey:@"cmtDesc"];
string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
[mainString appendFormat:@",\"%@\"",string];
string=[[commentArray objectAtIndex:i]objectForKey:@"cmtType"];
string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
[mainString appendFormat:@",\"%@\"",string];
[mainString appendFormat:@",\"%@\"",string];
[mainString appendFormat:@"\n"];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:@"Docment.conf"];
NSData* settingsData;
settingsData = [mainString dataUsingEncoding: NSASCIIStringEncoding];
if ([settingsData writeToFile:filePath atomically:YES])
NSLog(@"writeok");
您还可以使用https://github.com/davedelong/CHCSVParser生成CSV。
答案 1 :(得分:2)
Dave DeLong编写了一个CSV解析器,可以帮助您完成此任务,您可以在此问题中获得有关此答案的更多信息。How to convert the NSMutableArray to CSV file on iPhone?