使用 UserDefaults 保存和检索布尔值

Saving and retrieving a bool with UserDefaults

我正在尝试将 bool 值从 UISwitch 保存到 UserDefaults,并在另一个视图中检索它。但是,我尝试了多个教程和堆栈答案,none 似乎有效。

我是这样保存的:

class SettingsViewController: UITableViewController {

@IBOutlet weak var soundSwitchOutlet: UISwitch!

@IBAction func soundSwitch(_ sender: UISwitch) {

    UserDefaults.standard.set(soundSwitchOutlet.isOn, forKey: "sound")

}

这就是我尝试在另一个视图中检索它的方式:

if let savedValue = UserDefaults.standard.bool(forKey: "sound") {
        boolValue = savedValue
    }

//this is inside viewDidLoad and "boolValue" was declared outside viewDidLoad//

出于某种原因,此代码会给我错误,并且 none 我尝试过的方法都奏效了。如何将布尔值保存到 UserDefaults 并在另一个视图中检索它?

编辑:我想我修复了第一部分。但是,我检索布尔值的方式似乎是完全错误的。另外:没有其他 stackExchange 答案响应我的问题,至少在 swift.

中没有

使用这行代码:

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

而不是:

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

正如 Leo 在评论中提到的 bool(forKey returns 一个非可选的 Bool。如果密钥不存在,则返回 false

所以很简单

boolValue = UserDefaults.standard.bool(forKey: "sound")

不需要按照其他答案中的建议调用 synchronize()。该框架定期更新用户默认数据库。

这样做。

在你的first view controller.

  • 创建与您的 UISwitch

  • IBoutlet 连接
  • 然后是 UISwitch 的动作。所以最后,你的 first view controller 应该是这样的。

导入 UIKit

class FirstViewController: UIViewController {


    @IBOutlet weak var myswitch: UISwitch! // Outlet connection to your UISwitch (just control+ drag  it to your controller)




    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    @IBAction func myswitchAction(_ sender: Any) { // Action for your UISwitch

        var myswitctBool : Bool = false // create a local variable that holds your bool value. assume that in the beginning your switch is offed and the boolean value is `false`

        if myswitch.isOn == true { // when user turn it on then set the value to `true`
            myswitctBool = true
        }
        else { // else set the value to false
            myswitctBool = false
        }


        // finally set the value to user default like this
        UserDefaults.standard.set(myswitctBool, forKey: "mySwitch")
        //UserDefaults.standard.synchronize() - this is not necessary with iOS 8 and later.


    }

}

End of the first view controller

现在在你的第二个视图控制器中

  • 你可以得到你在first view controller中设置的userdefault的值。我把它放在 viewdidload 方法中来向您展示它是如何工作的。

导入 UIKit

class SecondViewController: UIViewController {

        override func viewDidLoad() {
            super.viewDidLoad()


            let myswitchBoolValuefromFirstVc : Bool = UserDefaults.standard.bool(forKey: "mySwitch")// this is how you retrieve the bool value


            // to see the value, just print those with conditions. you can use those for your things.
            if myswitchBoolValuefromFirstVc == true {
                print("true")
            }
            else {
                print("false")
            }


        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()

        }




    }

希望对您有所帮助。祝你好运

试试这个:

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

  //this is inside viewDidLoad and "boolValue" was declared outside viewDidLoad//

 boolValue = UserDefaults.standard.bool(forKey: "sound")