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")
}
答案 0 :(得分:9)
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")
}
答案 1 :(得分:9)
在Swift 3.0中
let defaults = UserDefaults.standard
它会创建标准默认文件并在其不存在时进行配置,如果不存在则将其打开。
<击> 撞击>
<击>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")
}
感谢@Fred纠正答案: - )
答案 2 :(得分:5)
接受的答案是正确的。这就是我喜欢的方式(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)
}
}
}
答案 3 :(得分: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{
}
答案 4 :(得分:0)
要检查bool值而不是 objectForKey ,我们必须使用 的 boolForKey 即可。我发现你接受的答案是错误的
if (NSUserDefaults.standardUserDefaults().boolForKey("onoroff")){
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "onoroff")
}else
{
NSUserDefaults.standardUserDefaults().setBool(false, forKey: "onoroff")
}
答案 5 :(得分:0)
Swift 3
这是交换机的代码,可以将真或假保存到UserDefaults。
let defaults = UserDefaults.standard
@IBOutlet weak var mySwitch: UISwitch!
@IBAction func switchValueChanged(_ sender: Any) {
self.defaults.set(mySwitch.isOn, forKey: "mySwitch")
}
然后显示开关的保存状态......开/关:
override func viewDidLoad() {
super.viewDidLoad()
mySwitch.isOn = self.defaults.bool(forKey: "mySwitch")
}
设置交换机的状态真的是1行,1行是获取用户保存状态并正确设置开关!