12

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.

if(![[NSUserDefaults standardUserDefaults] boolForKey:@"onoroff"]) {
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"onoroff"];
}

this is what I have so far in Swift...

if  NSUserDefaults.standardUserDefaults().objectForKey("onoroff") != nil{
    NSUserDefaults.standardUserDefaults().setBool(false, forKey: "onoroff")
}

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:

if  !NSUserDefaults.standardUserDefaults().boolForKey("onoroff"){
     NSUserDefaults.standardUserDefaults().setBool(true, forKey: "onoroff")
}else{ NSUserDefaults.standardUserDefaults().setBool(false, forKey: "onoroff")
     }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Emmanuel Amaro
  • 157
  • 1
  • 1
  • 8

6 Answers6

11
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")
}
CoderJ13
  • 117
  • 1
  • 13
V.J.
  • 9,492
  • 4
  • 33
  • 49
  • Thanks for your reply. What is more correct to use for a boolean, objectForKey or boolForKey? – Emmanuel Amaro Jun 26 '15 at 10:03
  • Both returns same result. But objectForKey is also used to check the key is available in the dictionary or not. – V.J. Jun 26 '15 at 10:05
  • Just for reference when i change my code to objectForKey Xcode gives me error and wants to change the code to !(NSUserDefaults.standardUserDefaults().objectForKey("onoroff") != nil) and not when i use boolForKey. So for simplicity i am going to stick with boolForKey. – Emmanuel Amaro Jun 26 '15 at 10:11
  • Actually i am not using swift. I gave you just an idea which is working in objective-c. – V.J. Jun 26 '15 at 10:13
8

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:-)

Ashok R
  • 19,892
  • 8
  • 68
  • 68
  • 2
    You create a property but you don't use it in your answer? if !defaults.bool(forKey: "onoroff") { defaults.set(true, forKey: "onoroff") } else { defaults.set(false, forKey: "onoroff") } – FredFlinstone Jul 21 '17 at 06:55
5

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)
    }

}

}

Jovan Stankovic
  • 4,661
  • 4
  • 27
  • 16
3
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{

}
iOS Dev
  • 195
  • 9
0

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")
        }
Nischal Hada
  • 3,230
  • 3
  • 27
  • 57
0

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!

Mike
  • 111
  • 9