To set an initial value for NSUserDefaults
in Swift, you can use the registerDefaults
method in your AppDelegate
‘s application(_:didFinishLaunchingWithOptions:)
method. This method allows you to set default values that will be used if a value hasn’t been explicitly set yet. Here’s how you can do it:
swiftfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let defaults = UserDefaults.standard
// Set the default value for "SoundActive" key to true
let defaultValues = ["SoundActive": true]
defaults.register(defaults: defaultValues)
return true
}
With this code, the default value for the key “SoundActive” will be set to true if the user hasn’t explicitly set it yet. If the user does change the value later, the new value will be used instead of the default.
Alternatively, you can also set default values in a plist file. To do this, create a new file named Defaults.plist
in your project and add the default values there. Then, in your AppDelegate
, load the plist file and set the default values like this:
swiftfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let defaults = UserDefaults.standard
if let path = Bundle.main.path(forResource: "Defaults", ofType: "plist"),
let dict = NSDictionary(contentsOfFile: path) as? [String: Any] {
defaults.register(defaults: dict)
}
return true
}
Make sure to replace "Defaults"
with the name of your plist file.
المزيد من المعلومات
To set an initial value for NSUserDefaults
in Swift, you can use the registerDefaults
method in your AppDelegate
‘s application(_:didFinishLaunchingWithOptions:)
method. This method allows you to set default values that will be used if a value hasn’t been explicitly set yet. Here’s how you can do it:
swiftfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let defaults = UserDefaults.standard
// Set the default value for "SoundActive" key to true
let defaultValues = ["SoundActive": true]
defaults.register(defaults: defaultValues)
return true
}
With this code, the default value for the key “SoundActive” will be set to true if the user hasn’t explicitly set it yet. If the user does change the value later, the new value will be used instead of the default.
Alternatively, you can also set default values in a plist file. To do this, create a new file named Defaults.plist
in your project and add the default values there. Then, in your AppDelegate
, load the plist file and set the default values like this:
swiftfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let defaults = UserDefaults.standard
if let path = Bundle.main.path(forResource: "Defaults", ofType: "plist"),
let dict = NSDictionary(contentsOfFile: path) as? [String: Any] {
defaults.register(defaults: dict)
}
return true
}
Make sure to replace "Defaults"
with the name of your plist file.