title: Saving bool to NSUserDefaults and using it in a if statement using Swift
body: <p>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 confirmation in that would be greatly appreciated. This is the Objective-C code I am trying to figure out how to use with swift.</p>
  
  <pre><code>if(![[NSUserDefaults standardUserDefaults] boolForKey:@"onoroff"]) {
      [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"onoroff"];
  }
  </code></pre>
  
  <p>this is what I have so far in Swift...</p>
  
  <pre><code>if  NSUserDefaults.standardUserDefaults().objectForKey("onoroff") != nil{
      NSUserDefaults.standardUserDefaults().setBool(false, forKey: "onoroff")
  }
  </code></pre>
  
  <p>I think I might have figure it out. Can anyone confirm this is the correct way to save a bool to NSUserDefaults and use a if statement with it. Here it is:</p>
  
  <pre><code>if  !NSUserDefaults.standardUserDefaults().boolForKey("onoroff"){
       NSUserDefaults.standardUserDefaults().setBool(true, forKey: "onoroff")
  }else{ NSUserDefaults.standardUserDefaults().setBool(false, forKey: "onoroff")
       }
  </code></pre>