I wanna write a dictionary like '[NSString:NSDictionary]' into a plist file. But when I call -writeToFile: it always return me a False. The following is the data in dataDic:
Printing description of dataDic:
{
5 = {
category = 5;
img = “img”;
name = “sport”;
};
1 = {
category = 1;
img = “img”;
name = “music”;
};
}
and the following is my code:
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];
Try the below code
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 ");;
}
please let me know if this worked for you or not ..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.