使用UserDefaults保存和检索bool

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

这是我如何保存它:

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// 

出于某种原因,这段代码给了我错误,而且我尝试过的东西都没有。 如何将bool保存到UserDefaults并在另一个视图中检索它?

编辑:我想我修复了第一部分。 但是,我检索布尔值的方式似乎完全错误。 另外:没有其他stackExchange答案回应我的要求,至少不是迅速。

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

所以这很简单

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

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

像这样做。

在你的first view controller

  • 创建与UISwitchIBoutlet连接

  • 然后是你的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. } } 

第一个视图控制器结束

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

  • 您可以获取在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 soundSwitch(_ sender: UISwitch) { UserDefaults.standard.set(soundSwitchOutlet.isOn, forKey: "sound") } 

代替 :

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

尝试这个:

 @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")