我正在尝试修改plist并保存更改,但它不起作用:
这是我的代码:
NSString *myPath = [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"];
NSMutableDictionary *plistDict;
NSMutableArray *array = [[NSMutableArray alloc]initWithArray:[plistDict objectForKey:@"$objects"]];
for (int i =0 ; i <array.count; i++)
{
if ([[array objectAtIndex:i] isKindOfClass:[NSString class]])
{
if ([[array objectAtIndex:i] isEqualToString:@"myString"])
{
[[plistDict objectForKey:@"$objects"]replaceObjectAtIndex:i withObject:@"newString"];
}
}
}
NSString *root = @"/Users/myUser/Desktop/newPlist.plist";
[plistDict writeToFile:root atomically:YES];
内容的字典已被修改,我没有任何错误,但文件从未在路径中创建。你们中的任何人都知道我做错了什么?或者我该如何修复?
答案 0 :(得分:0)
我怀疑您正在写一个沙盒位置。实际上并不是用户目录。
答案 1 :(得分:0)
参考以下代码: -
NSMutableDictionary *plistDict=//assuming your data
NSString *root = @"/Users/home/Desktop/newPlist.plist";
if (![[NSFileManager defaultManager]fileExistsAtPath:root])//checking fileexist or not
{
//if not exist creating the same
[[NSFileManager defaultManager]createFileAtPath:root contents:nil attributes:nil];
[plistDict writeToFile:root atomically:YES];
}
else
{
[plistDict writeToFile:root atomically:YES];
}