初始值 Swift UserDefault
Initital value Swift UserDefault
我想知道是否有办法为 NSUserDefaults 设置起始值
我想在 UserDefault 的开头有一个值,而不是在后面更改它,除非重新安装应用程序,否则永远不要将其重置为初始值。
我在网上进行了搜索,但没有找到任何内容。
有办法。文档中对此进行了描述
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
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Register the preference defaults early.
let appDefaults = ["CacheDataAgressively" : true]
NSUserDefaults.standardUserDefaults().registerDefaults(appDefaults)
// Other initialization...
}
我想知道是否有办法为 NSUserDefaults 设置起始值 我想在 UserDefault 的开头有一个值,而不是在后面更改它,除非重新安装应用程序,否则永远不要将其重置为初始值。
我在网上进行了搜索,但没有找到任何内容。
有办法。文档中对此进行了描述
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 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 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Register the preference defaults early. let appDefaults = ["CacheDataAgressively" : true] NSUserDefaults.standardUserDefaults().registerDefaults(appDefaults) // Other initialization... }