xcode 如何立即在 iPhone 上存储设置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9034750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:07:26  来源:igfitidea点击:

How to store settings on iPhone immediately?

iphonexcodeipadios4nsuserdefaults

提问by Dmitry

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?

是否可以立即将设置存储在 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"];
}

回答by Andrey

[[NSUserDefaults standardUserDefaults] synchronize];

[[NSUserDefaults standardUserDefaults] 同步];

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.

同步 - 此方法会定期自动调用,仅当您无法等待自动同步(例如,如果您的应用程序即将退出)或者您想将用户默认值更新为磁盘上的内容时才使用此方法虽然你没有做任何改变。

回答by wattson12

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.

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

[[NSUserDefaults standardUserDefaults] synchronize];

回答by X Slash

Call your compSavein your AppDelegate

打电话给你compSaveAppDelegate

- (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];
}

回答by DarkDust

Simply call [[NSUserDefaults standardUserDefaults] synchronize];. But usually you do notneed to that: iOS will save once your app goes into background and also on a few other occasions. Calling synchronizetoo often is a considered to be a Bad Thing(tm).

只需调用[[NSUserDefaults standardUserDefaults] synchronize];. 但通常你并不需要的是:iOS版将节省一旦你的程序被切换到后台还取决于其他一些场合。打电话synchronize太频繁被认为是一件坏事(tm)。

回答by petert

Try using the synchronizemethod.

尝试使用该synchronize方法。

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

更新:你应该考虑注册你的默认值,就像它在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.

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

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

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