简体   繁体   English

使用NSUserdefaults Swift首次加载整数错误

[英]Loading Integer For The First Time Error Using NSUserdefaults Swift

When I got to a page with this code: 当我进入带有此代码的页面时:

override func viewDidLoad() {
super.viewDidLoad()

//Load Upgrades

    let FourthDefaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
    var upgrades = FourthDefaults.valueForKey("Upgrades")?.integerValue
    FourthDefaults.synchronize()
    Upgrades = upgrades!
}

I am getting an EXC_BAD_INSTRUCTIONS error on: 我在以下位置收到EXC_BAD_INSTRUCTIONS错误:

Upgrades = upgrades!

In case this helps, this is my saving method. 如果有帮助,这是我的保存方法。

let FourthDefaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
        FourthDefaults.setObject(Upgrades, forKey: "Upgrades")
        FourthDefaults.synchronize()

Extra Info: This only happens when I have this code load before I save it. 额外信息:仅当我在保存之前加载此代码时,才会发生这种情况。 Otherwise it works fine. 否则,它将正常工作。

If you know how to fix this please respond. 如果您知道如何解决此问题,请回复。 Thanks! 谢谢!

valueForKey returns an optional. valueForKey返回一个可选。 If there is no value for the key “upgrades”, the result will be nil . 如果键“ upgrades”没有值,则结果为nil This will be the case before you save the value for the first time. 在首次保存该值之前,情况就是这样。

This is why you have to write valueForKey("Upgrades")?.integerValue – because if the value returned is nil instead of an AnyObject , you can't call .integerValue on it. 这就是为什么您必须编写valueForKey("Upgrades")?.integerValue原因–因为如果返回的值为nil而不是AnyObject ,则无法对其调用.integerValue You have to use the ?. 您必须使用?. operator. 运营商。 Read that operator as “if the result of this is nil , then stop and return nil , otherwise continue and return an optional containing the result of the next method.” 将该运算符读为“如果其结果为nil ,则停止并返回nil ,否则继续并返回一个包含next方法的结果的可选内容。”

So upgrades is either nil (if there was no such key), or an optional containing the value you want. 因此, upgrades要么nil (如果没有这样的密钥),要么为包含所需值的可选项。

If you force-unwrap an optional using ! 如果使用强制打开可选项! , and it is nil , you will get a runtime assertion, like you're getting. ,它为nil ,您将获得运行时断言,就像您得到的一样。 For this reason you should hardly ever use ! 因此,您几乎不应该使用! . The times you do need to use it are pretty rare and you should think carefully about whether there's an alternative. 您确实需要使用它的时间很少,因此您应该仔细考虑是否有替代方法。

In this case there is a much better alternative – use the ?? 在这种情况下,有更好的选择–使用?? operator: 运营商:

var upgrades = FourthDefaults.valueForKey("Upgrades")?.integerValue ?? 0

(or whatever default value you want in the case of a missing key, if 0 isn't it) (或者在缺少键的情况下您想要的任何默认值,如果不是0的话)

The ?? ?? reads as: if the expression on the left is nil , then substitute the value on the right instead. 读取为:如果左侧的表达式为nil ,则替换右侧的值。 Otherwise, use the unwrapped value on the left. 否则,请使用左侧的展开值。

This has the benefit of you not needing the ! 这样的好处是您不需要! anymore, and should eliminate your runtime error. 并应消除您的运行时错误。

Alternatively, if you want to execute different code when the value hasn't already been set, you could write this: 或者,如果要在尚未设置该值的情况下执行其他代码,则可以编写以下代码:

if let upgrades = FourthDefaults.valueForKey("Upgrades")?.integerValue {
    // use upgrades, which will be unwrapped in this block
}
else {
    // handle the value not being set yet
}

For more on why optionals are useful, try reading this answer . 有关为什么可选选项有用的更多信息,请尝试阅读此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM