如何立即保存 iPhone 上的设置?

发布于 2024-12-29 05:53:02 字数 664 浏览 6 评论 0原文

是否可以立即在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深海少女心 2025-01-05 05:53:02

[[NSUserDefaults standardUserDefaults]同步];

synchronize - 该方法会定期自动调用
间隔,仅当您无法等待自动执行时才使用此方法
同步(例如,如果您的应用程序即将退出)或
如果您想将用户默认值更新为磁盘上的内容,即使
您尚未进行任何更改。

[[NSUserDefaults standardUserDefaults] synchronize];

synchronize - this method is automatically invoked at periodic
intervals, use this method only if you cannot wait for the automatic
synchronization (for example, if your application is about to exit) or
if you want to update the user defaults to what is on disk even though
you have not made any changes.

清风挽心 2025-01-05 05:53:02

根据文档,以下调用将保留任何未完成的修改。要检查此操作是否成功,请检查此调用的返回值。值为 YES 表示成功。

[[NSUserDefaults standardUserDefaults] synchronize];

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.

[[NSUserDefaults standardUserDefaults] synchronize];
晒暮凉 2025-01-05 05:53:02

AppDelegate 中调用 compSave

- (void)applicationWillResignActive:(UIApplication *)application 
{
    // Call your compSave here to force saving before app is going to the background
}

还可以

- (void)compSave 
{
    [[NSUserDefaults standardUserDefaults] setInteger: compTotal forKey: @"compTotal"];
    [[NSUserDefaults standardUserDefaults] setInteger: compCount forKey: @"compCount"];
    [[NSUserDefaults standardUserDefaults] setBool: compShow forKey: @"compShow"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

Call your compSave in your AppDelegate

- (void)applicationWillResignActive:(UIApplication *)application 
{
    // Call your compSave here to force saving before app is going to the background
}

also

- (void)compSave 
{
    [[NSUserDefaults standardUserDefaults] setInteger: compTotal forKey: @"compTotal"];
    [[NSUserDefaults standardUserDefaults] setInteger: compCount forKey: @"compCount"];
    [[NSUserDefaults standardUserDefaults] setBool: compShow forKey: @"compShow"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
墨洒年华 2025-01-05 05:53:02

只需调用[[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. Calling synchronize too often is a considered to be a Bad Thing(tm).

¢蛋碎的人ぎ生 2025-01-05 05:53:02

尝试使用synchronize方法。

更新:您应该考虑注册默认值,就像 首选项和设置编程指南

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   // Register the preference defaults early.
   NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:@"CacheDataAgressively"];
   [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];

   // Other initialization...
}

此外,根据本指南(“同步和检测偏好更改”),关于同步:

要检测首选项值何时发生更改,应用程序还可以注册通知 NSUserDefaultsDidChangeNotification。每当共享 NSUserDefaults 对象检测到位于某个持久域中的首选项发生更改时,它就会将此通知发送到您的应用程序。您可以使用此通知来响应可能影响您的用户界面的更改。例如,您可以使用它来检测用户首选语言的更改并相应地更新您的应用内容。

如果您想直接重新读取设置,可以肯定您仍然需要使用synchronize

Try using the synchronize method.

Update: you should consider registering your defaults, like it suggests in the Preferences and Settings Programming Guide:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   // Register the preference defaults early.
   NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:@"CacheDataAgressively"];
   [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];

   // Other initialization...
}

Also, from this guide ("Synchronizing and Detecting Preference Changes"), regarding syncing:

To detect when changes to a preference value occur, apps can also register for the notification NSUserDefaultsDidChangeNotification. The shared NSUserDefaults object sends this notification to your app whenever it detects a change to a preference located in one of the persistent domains. You can use this notification to respond to changes that might impact your user interface. For example, you could use it to detect changes to the user’s preferred language and update your app content appropriately.

Pretty sure you still need to use synchronize if you want to re-read settings directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文