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

后端 未结 6 1267
遇见更好的自我
遇见更好的自我 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: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")
            }
    

提交回复
热议问题