使用 NSUserDefaults 在构建时存储默认值
Using NSUserDefaults to store default values at build time
我正在存储一个默认值的小字典,用户可以稍后修改,但可能只会更改一次。 NSUserDefaults.standardUserDefaults 似乎是存储这种东西的正确位置,我的问题是:有没有办法在构建时而不是运行时存储值?这段代码看起来应该是不必要的。
if !NSUserDefaults.standardUserDefaults().dictionaryForKey(default) {
NSUserDefaults.standardUserDefaults().setObject(defaultDictionary, forKey: "default")
}
或者,我应该考虑有更好的选择吗?
为了在构建时存储值,我会创建一个 .plist 或 .json 文件并预先填充您要存储的值,然后可以在运行时访问这些值。
中所述注册默认值
Registering Your App’s Default Preferences
At launch time, an app should register default values for any
preferences that it expects to be present and valid. When you request
the value of a preference that has never been set, the methods of the
NSUserDefaults
class return default values that are appropriate for
the data type. For numerical scalar values, this typically means
returning 0, but for strings and other objects it means returning nil
.
If these standard default values are not appropriate for your app, you
can register your own default values using the registerDefaults:
method. This method places your custom default values in the
NSRegistrationDomain
domain, which causes them to be returned when a
preference is not explicitly set.
When calling the registerDefaults:
method, you must provide a
dictionary of all the default values you need to register. Listing 2-1
shows an example where an iOS app registers its default values early
in the launch cycle. You can register default values at any time, of
course, but should always register them before attempting to retrieve
any preference values.
Listing 2-1 Registering default preference values
- (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...
}
When registering default values for scalar types, use an NSNumber
object to specify the value for the
number. If you want to register a preference whose value is a URL, use
the archivedDataWithRootObject:
method of NSKeyedArchiver
to encode
the URL in an NSData
object first. Although you can use a similar
technique for other types of objects, you should avoid doing so when a
simpler option is available.
我正在存储一个默认值的小字典,用户可以稍后修改,但可能只会更改一次。 NSUserDefaults.standardUserDefaults 似乎是存储这种东西的正确位置,我的问题是:有没有办法在构建时而不是运行时存储值?这段代码看起来应该是不必要的。
if !NSUserDefaults.standardUserDefaults().dictionaryForKey(default) {
NSUserDefaults.standardUserDefaults().setObject(defaultDictionary, forKey: "default")
}
或者,我应该考虑有更好的选择吗?
为了在构建时存储值,我会创建一个 .plist 或 .json 文件并预先填充您要存储的值,然后可以在运行时访问这些值。
Registering Your App’s Default Preferences
At launch time, an app should register default values for any preferences that it expects to be present and valid. When you request the value of a preference that has never been set, the methods of the
NSUserDefaults
class return default values that are appropriate for the data type. For numerical scalar values, this typically means returning 0, but for strings and other objects it means returningnil
. If these standard default values are not appropriate for your app, you can register your own default values using theregisterDefaults:
method. This method places your custom default values in theNSRegistrationDomain
domain, which causes them to be returned when a preference is not explicitly set.When calling the
registerDefaults:
method, you must provide a dictionary of all the default values you need to register. Listing 2-1 shows an example where an iOS app registers its default values early in the launch cycle. You can register default values at any time, of course, but should always register them before attempting to retrieve any preference values.Listing 2-1 Registering default preference values
- (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... }
When registering default values for scalar types, use an
NSNumber
object to specify the value for the number. If you want to register a preference whose value is a URL, use thearchivedDataWithRootObject:
method ofNSKeyedArchiver
to encode the URL in anNSData
object first. Although you can use a similar technique for other types of objects, you should avoid doing so when a simpler option is available.