NSUserDefaults setValue:删除我的plist。发生了什么事?
不知何故,下面提到的代码似乎删除了我的 ~/Library/Preferences/NAME.PRODUCT.plist
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:NO forKey:@"ViewFirstShown"];
[defaults synchronize];
我可以在调用这些行之前确认 plist 文件是否存在。但是运行上述代码后plist文件立即消失。
我认为同步会立即将值写入磁盘。我也尝试等待一段时间,但 plist 没有重新出现。
我错过了什么吗?
提前致谢。
Somehow, the below mentioned code seems to delete my ~/Library/Preferences/NAME.PRODUCT.plist
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:NO forKey:@"ViewFirstShown"];
[defaults synchronize];
I can confirm the plist file exists before these lines are called. But the plist file disappears immediately after running the above codes.
I thought synchronise
writes the values to the disk
immediately. I tried waiting a while too, but the plist just doesn't re-appear.
Am I missing anything?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里没有足够的信息来真正回答您的问题。
执行上述代码时,“ViewFirstShown”键是首选项 plist 中的唯一键吗?如果是这样,则
-setValue:forKey:
调用将删除首选项中的最后一个键,这将删除首选项 plist。另一件需要注意的事情是,您使用的是
-[NSUserDefaults setValue:forKey:]
,它采用id
作为值。因为NO
是0L
并且实际上是nil
,因此您将删除此处的任何值。您可能想改用-[NSUserDefaults setBool:forKey:]
。There isn't quite enough information to really answer your question here.
Is the "ViewFirstShown" key the only key in the preferences plist at the time the code above is executed? If so, then the
-setValue:forKey:
call is removing the last key in the prefs, which will remove the preferences plist.One other thing to note is that you're using
-[NSUserDefaults setValue:forKey:]
which takes anid
for the value. BecauseNO
is0L
and that's effectivelynil
you're removing any value that's here. You probably want to be using-[NSUserDefaults setBool:forKey:]
instead.