我是编程的新手,我得到了一个我无法解决的错误,即使这个问题也没有帮助我:“致命错误:在解开一个Optional值时意外发现nil”是什么意思?
这是错误:
致命错误:在展开Optional值时意外发现nil 2017-10-27 18:30:48.283483 + 0200 Inspireme1.0 [1149:631849]致命错误:在解包可选值时意外发现nil (lldb)
这是我的代码:
var quotes: RandomItems! = RandomItems([
"Jonas",
"Mary",
"Michael",
"Jeff",
"Sarah",
])
@IBAction func PresentText(_ sender: Any) {
PresentingLabel.text = quotes.next() //<-- Error
}
var randomItems: RandomItems?
override func viewDidAppear(_ animated: Bool) {
// Code to load the struct again after the view appears.
let defaults = UserDefaults.standard
quotes = defaults.codable(RandomItems.self, forKey: "quotes")
}
override func viewWillDisappear(_ animated: Bool) {
// Code to save struct before the view disappears.
let defaults = UserDefaults.standard
if let quotes = quotes {
defaults.set(codable: quotes, forKey: "quotes")
}
}