繁体   English   中英

更改plist中的值-不更改值

[英]Changing values in plist - Doesn't change value

我正在使用这个发现写给plist的简单方法,但是它似乎不起作用:

-(IBAction)modifyPlist:(id)sender{

NSBundle *mainBundle=[NSBundle mainBundle];
NSString *path=[mainBundle pathForResource:@"Preferences" ofType:@"plist"];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:path];
[[dict objectForKey:@"Root"] setBool:YES forKey:@"startup"];
}

我究竟做错了什么? 谢谢

您正在将文件加载到内存中,并修改该内存,但没有将其写回到磁盘。 您将要在-writeToFile:atomically:上使用NSDictionary-writeToFile:atomically:

[dict writeToFile:path atomically:YES];

参考链接在这里

希望您确切知道自己在做什么,这是(无论出于何种原因)您的设计决定-
因为您将要写入应用程序的bundle

在Sandboxing下肯定会失败,这通常是一个坏主意。

如果您已经存储了用户默认设置,请在Mac上阅读存储首选项的方式:
首选项和设置编程指南

// Make a path to the plist in the Documents directory (not the bundle directory)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Preferences.plist"];

// Then after creating or making changes to your dictionary, write your dictionary out to the plist file
[dict writeToFile:path atomically:YES];

也许看一下: 我应该使用NSUserDefaults还是plist来存储数据?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM