Swift 4 Settings Bundle, get the default settings

I created a preset with about 8 switches. What I'm trying to do is get the default values ​​from a set of parameters. I now have two methods:

func registerSettingsBundle(){
        let appDefaults = [String:AnyObject]()
        UserDefaults.standard.register(defaults: appDefaults)
        UserDefaults.standard.synchronize()
    }

    func updateDisplayFromDefaults(){
        let defaults = UserDefaults.standard
        let update_lot = defaults.bool(forKey: "update_lot")
        print(update_lot)
    }

and I call these methods in my DidLoad view

override func viewDidLoad() {
        super.viewDidLoad()
        registerSettingsBundle()
        updateDisplayFromDefaults()
    }

However, this does not give me default values ​​(which are true, but they all return false). This works and gives me the correct values ​​if I close the application, open the settings, configure the settings and open the application again. Can't get the default settings? I went the way of reading plist, but if I changed the settings in my set of settings, this did not take effect.

+6
source share
4

( , all return false)

, , ON , , .

, - .

(Root.plist) " " , . . " ", "" plist, , false.

enter image description here

Root.plist , , .

- false.

func getDefaults() {        
    let stanDefaults = UserDefaults.standard
    print("Default value of Update - \(stanDefaults.bool(forKey: "update_lot_pref"))")
    print("\nDefault value of Reminder - \(stanDefaults.bool(forKey: "reminder_pref"))")
}

Update - false - false

, - Root.plist - .

func setApplicationDefault() {
    let stanDefaults = UserDefaults.standard
    let appDefaults = ["reminder_pref": true]
    stanDefaults.register(defaults: appDefaults)
    stanDefaults.synchronize()
}

, Root.plist, , viewDidload true. ,

- true

Root.plist.

enter image description here

, .

+2

, . , YES, , NO.

Settings. Required Content

, Settings.bundle, UserDefaults , . , iOS , .

Root.plist, Settings.bundle, .

func registerDefaultsFromSettingsBundle()
{
    let settingsUrl = Bundle.main.url(forResource: "Settings", withExtension: "bundle")!.appendingPathComponent("Root.plist")
    let settingsPlist = NSDictionary(contentsOf:settingsUrl)!
    let preferences = settingsPlist["PreferenceSpecifiers"] as! [NSDictionary]

    var defaultsToRegister = Dictionary<String, Any>()

    for preference in preferences {
        guard let key = preference["Key"] as? String else {
            NSLog("Key not fount")
            continue
        }
        defaultsToRegister[key] = preference["DefaultValue"]
    }
    UserDefaults.standard.register(defaults: defaultsToRegister)
}

. , .

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
    registerDefaultsFromSettingsBundle()

    let one = UserDefaults.standard.bool(forKey: "switch_one")
    let two = UserDefaults.standard.bool(forKey: "switch_two")

    NSLog("One: \(one), Two: \(two)")

    return true
}
+3

UserDefaults.didChangeNotification, :

override func viewDidLoad() {
    super.viewDidLoad()
    registerSettingsBundle()
    NotificationCenter.default.addObserver(self, selector: #selector(updateDisplayFromDefaults), name: UserDefaults.didChangeNotification, object: nil)
    updateDisplayFromDefaults()
}
func registerSettingsBundle(){
    let appDefaults = [String:AnyObject]()
    UserDefaults.standard.register(defaults: appDefaults)
    UserDefaults.standard.synchronize()
}

func updateDisplayFromDefaults(){
    let defaults = UserDefaults.standard
    let update_lot = defaults.bool(forKey: "update_lot")
    print(update_lot)
}
+2

, , , , , , "", undefined ( , true). , :

let update_lot = defaults.bool(forKey: "update_lot")

... for this code, there is no way to know that "unset" should be evaluated as "true" and not "false". Instead, you can use defaults.object(forkey: "update_lot"), and if this returns an object, get the logical value of that object (or just call defaults.boolat that point), but if it returns nil, suppose it is true.

0
source

All Articles