问题
I'm saving a few user settings in NSUserDefaults
. If these settings have not been modified by the user, I'd like the app to use presets.
How should I set this up so the app first checks if settings have been modified and if not, sets presets?
Checking if [[NSUserDefaults standardDefaults]objectForKey: @"setting1"] == nil
? Then setting a preset if it is not?
回答1:
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html#//apple_ref/doc/uid/20000318-CIHDDCDB
Call that from your applicationDidFinishLaunchingWithOptions:
method that sets up whatever defaults you may need.
回答2:
Look at the NSUserDefaults docs for registerDefaults. This allows you to have default values until the user changes them, then they have no effect.
来源:https://stackoverflow.com/questions/11870648/nsuserdefaults-intitial-setup