Saving bool to NSUserDefaults and using it in a if statement using Swift

后端 未结 6 1264
遇见更好的自我
遇见更好的自我 2021-02-19 21:44

I am struggling to figure out how to do an if statement with a bool saved to NSUserDefaults using Swift. I believe I know how to save the bool to NSUserDefaults but a confirmati

相关标签:
6条回答
  • 2021-02-19 22:12
    NSUserDefaults.standardUserDefaults().setObject(Bool(false), forKey:"onoroff")
    
    var onoroff = NSUserDefaults.standardUserDefaults().objectForKey("onoroff") as Bool!
    
    if (onoroff != nil && onoroff == false)
    {
    
    }
    

    EDIT:

    NSUserDefaults.standardUserDefaults().setBool(false, forKey: "onoroff")
    
    if NSUserDefaults.standardUserDefaults().boolForKey("onoroff"){
    
    }else{
    
    }
    
    0 讨论(0)
  • 2021-02-19 22:12

    Swift 3

    This is code for the switch, that will save true or false to UserDefaults.

    let defaults = UserDefaults.standard
    @IBOutlet weak var mySwitch: UISwitch!
    
    @IBAction func switchValueChanged(_ sender: Any) {
        self.defaults.set(mySwitch.isOn, forKey: "mySwitch")
    }
    

    Then to show the switch's saved state... on/off:

    override func viewDidLoad() {
        super.viewDidLoad() 
        mySwitch.isOn = self.defaults.bool(forKey: "mySwitch")
    }
    

    So really 1 line to set the switch's state, and 1 line to get the users saved state and set the switch properly!

    0 讨论(0)
  • 2021-02-19 22:20
    if NSUserDefaults.standardUserDefaults().boolForKey("onoroff") == true
    

    The above code is only check wheather the value of the key is true or not.

    or

    if NSUserDefaults.standardUserDefaults().objectForKey("onoroff")
    

    The above code check wheather the value of the key is true or not as well if there is no value for the key.(Means Nothing inserted in the UserDefault of "onoroff").

    EDIT

    if !NSUserDefaults.standardUserDefaults().objectForKey("onoroff")
    {
         NSUserDefaults.standardUserDefaults().setBool(true, forKey: "onoroff")
    }
    else
    {
        NSUserDefaults.standardUserDefaults().setBool(false, forKey: "onoroff")
    }
    
    0 讨论(0)
  • 2021-02-19 22:22

    To check bool value instead of objectForKey we must use boolForKey. I find that as a mistakes in the answer you accepted

           if (NSUserDefaults.standardUserDefaults().boolForKey("onoroff")){
                NSUserDefaults.standardUserDefaults().setBool(true, forKey: "onoroff")
            }else
            {
                NSUserDefaults.standardUserDefaults().setBool(false, forKey: "onoroff")
            }
    
    0 讨论(0)
  • 2021-02-19 22:33

    In Swift 3.0

     let defaults = UserDefaults.standard
    

    It creates the standard defaults file and configures it if it does not exist, or open it if it does.

    if !UserDefaults.standard.bool(forKey: "onoroff") {
                UserDefaults.standard.set(true, forKey: "onoroff")
         } else {
                UserDefaults.standard.set(false, forKey: "onoroff")
         }
    

    if !defaults.standard.bool(forKey: "onoroff") {
                defaults.standard.set(true, forKey: "onoroff")
         } else {
                defaults.standard.set(false, forKey: "onoroff")
         }
    

    Thanks you @Fred for correcting the answer:-)

    0 讨论(0)
  • 2021-02-19 22:36

    The accepted answer is correct. This is the way I like to do it (Swift 3.0):

    struct Settings {
    
    fileprivate struct Keys {
        static let soundKey = "soundKey"
    }
    
    static var sound: Bool {
        set {
            UserDefaults.standard.set(newValue, forKey: Keys.soundKey)
        }
        get {
            if UserDefaults.standard.object(forKey: Keys.soundKey) == nil {
                // Value for sound not yet set ... Setting default ...
    
                UserDefaults.standard.set(true, forKey: Keys.soundKey)
                return true
            }
            return UserDefaults.standard.bool(forKey: Keys.soundKey)
        }
    
    }
    

    }

    0 讨论(0)
提交回复
热议问题