如何立即保存 iPhone 上的设置?
是否可以立即在 iPhone 上存储设置? iOS 4+ 上的应用程序在退出时不会关闭,如果我在 Xcode 设置中按“停止进程”,则不会保存?如何拯救他们?
- (void)compLoad {
compTotal = [[NSUserDefaults standardUserDefaults] integerForKey: @"compTotal"];
compCount = [[NSUserDefaults standardUserDefaults] integerForKey: @"compCount"];
compShow = [[NSUserDefaults standardUserDefaults] boolForKey: @"compShow"];
}
- (void)compSave {
[[NSUserDefaults standardUserDefaults] setInteger: compTotal forKey: @"compTotal"];
[[NSUserDefaults standardUserDefaults] setInteger: compCount forKey: @"compCount"];
[[NSUserDefaults standardUserDefaults] setBool: compShow forKey: @"compShow"];
}
Is it possible to store settings on the iPhone immediately? The apps on iOS 4+ don't close on exit, and if I press 'Stop process' in Xcode settings are not saved? How to save them?
- (void)compLoad {
compTotal = [[NSUserDefaults standardUserDefaults] integerForKey: @"compTotal"];
compCount = [[NSUserDefaults standardUserDefaults] integerForKey: @"compCount"];
compShow = [[NSUserDefaults standardUserDefaults] boolForKey: @"compShow"];
}
- (void)compSave {
[[NSUserDefaults standardUserDefaults] setInteger: compTotal forKey: @"compTotal"];
[[NSUserDefaults standardUserDefaults] setInteger: compCount forKey: @"compCount"];
[[NSUserDefaults standardUserDefaults] setBool: compShow forKey: @"compShow"];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
[[NSUserDefaults standardUserDefaults]同步];
[[NSUserDefaults standardUserDefaults] synchronize];
根据文档,以下调用将保留任何未完成的修改。要检查此操作是否成功,请检查此调用的返回值。值为 YES 表示成功。
According to the documentation, the following call will persist any outstanding modifications. To check if this has been successful, check the return value of this call. A value of YES indicates success.
在
AppDelegate
中调用compSave
还可以
Call your
compSave
in yourAppDelegate
also
只需调用
[[NSUserDefaults standardUserDefaults]同步];
。但通常您不需要这样做:一旦您的应用程序进入后台以及其他一些情况,iOS 就会保存。过于频繁地调用synchronize
被认为是一件坏事(tm)。Simply call
[[NSUserDefaults standardUserDefaults] synchronize];
. But usually you do not need to that: iOS will save once your app goes into background and also on a few other occasions. Callingsynchronize
too often is a considered to be a Bad Thing(tm).尝试使用
synchronize
方法。更新:您应该考虑注册默认值,就像 首选项和设置编程指南:
此外,根据本指南(“同步和检测偏好更改”),关于同步:
如果您想直接重新读取设置,可以肯定您仍然需要使用
synchronize
。Try using the
synchronize
method.Update: you should consider registering your defaults, like it suggests in the Preferences and Settings Programming Guide:
Also, from this guide ("Synchronizing and Detecting Preference Changes"), regarding syncing:
Pretty sure you still need to use
synchronize
if you want to re-read settings directly.