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

后端 未结 6 1266
遇见更好的自我
遇见更好的自我 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{
    
    }
    

提交回复
热议问题