ios - Need help saving bool to NSUserDefaults and using it in a if statement using Swift -
i struggling figure out how if statement bool saved nsuserdefaults using swift. believe know how save bool nsuserdefaults confirmation in appreciated. objective-c code trying figure out how use swift.
if(![[nsuserdefaults standarduserdefaults] boolforkey:@"onoroff"]) { [[nsuserdefaults standarduserdefaults] setbool:yes forkey:@"onoroff"]; }
this have far in swift...
if nsuserdefaults.standarduserdefaults().objectforkey("onoroff") != nil{ nsuserdefaults.standarduserdefaults().setbool(false, forkey: "onoroff") }
i think might have figure out. can confirm correct way save bool nsuserdefaults , use if statement it. here is...
if !nsuserdefaults.standarduserdefaults().boolforkey("onoroff"){ nsuserdefaults.standarduserdefaults().setbool(true, forkey: "onoroff") }else{ nsuserdefaults.standarduserdefaults().setbool(false, forkey: "onoroff") }
if nsuserdefaults.standarduserdefaults().objectforkey("onoroff") == true
the above code check wheather value of key true or not.
or
if nsuserdefaults.standarduserdefaults().objectforkey("onoroff")
the above code check wheather value of key true or not if there no value key.(means nothing inserted in userdefault of "onoroff").
edit
if !nsuserdefaults.standarduserdefaults().objectforkey("onoroff") { nsuserdefaults.standarduserdefaults().setbool(true, forkey: "onoroff") } else { nsuserdefaults.standarduserdefaults().setbool(false, forkey: "onoroff") }
Comments
Post a Comment