Saving and retrieving a bool with UserDefaults

前端 未结 4 1389
醉酒成梦
醉酒成梦 2021-01-13 01:33

I\'m trying to save a bool value to UserDefaults from a UISwitch, and retrieve it in another view. However, I\'ve tried following multiple tutorials and stack answe

4条回答
  •  广开言路
    2021-01-13 02:23

    Use this line of code:

    @IBAction func soundSwitch(_ sender: UISwitch) {
        UserDefaults.standard.set(soundSwitchOutlet.isOn, forKey: "sound")
    }
    

    insteadof :

    @IBAction func soundSwitch(_ sender: UISwitch) {
        UserDefaults.standard.set(soundSwitchOutlet, forKey: "sound")
    }
    

提交回复
热议问题