我想把'[NSString:NSDictionary]'这样的字典写成一个plist文件。但是当我打电话给 -writeToFile: 时,它总是给我一个假的。以下是dataDic中的数据:
Printing description of dataDic:
{
5 = {
category = 5;
img = “img”;
name = “sport”;
};
1 = {
category = 1;
img = “img”;
name = “music”;
};
}
以下是我的代码:
NSMutableDictionary *dataDic = [NSMutableDictionary dictionary];
for (NSDictionary* dict in arr) {
[dataDic setObject:dict forKey:dict[@"category"]];
}
NSString *plistPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;
NSString *filename=[plistPath stringByAppendingPathComponent:@"test.plist"];
NSFileManager* fm = [NSFileManager defaultManager];
[fm createFileAtPath:filename contents:nil attributes:nil];
BOOL boo = [dataDic writeToFile:filename atomically:YES];
答案 0 :(得分:0)
尝试以下代码
NSDictionary * dataDic = "YOUR_DICTIONARY"
NSString *error = nil;
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(plistData)
{
[plistData writeToFile:plistPath atomically:YES];
NSLog(@"Data saved sucessfully");
}
else
{
NSLog(@"Data not saved ");;
}
如果这对你有用,请告诉我。