我收到以下錯誤信息:迅速致命錯誤發現零
"fatal error: unexpectedly found nil while unwrapping an Optional value"
當我運行這部分代碼:
func askQuestion() {
button1.setImage(UIImage(named: cards[0]), forState: .Normal)
button2.setImage(UIImage(named: cards[1]), forState: .Normal)
button3.setImage(UIImage(named: cards[2]), forState: .Normal)
//title = cards[correctAnswer].uppercaseString
}
我不知道我做錯了或者問題是什麼。這是代碼的其餘部分:
import GameplayKit
import UIKit
class guessViewController: UIViewController {
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!
@IBOutlet weak var button4: UIButton!
var cards = [String]()
var score = 0
var correctAnswer = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
cards += ["boots", "tired", "heart", "angry", "leaf", "sleepy", "apple", "chestnuts", "pumpkin", "rainbow"]
askQuestion()
//cards = GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(cards) as! [String]
}
func askQuestion() {
button1.setImage(UIImage(named: cards[0]), forState: .Normal)
button2.setImage(UIImage(named: cards[1]), forState: .Normal)
button3.setImage(UIImage(named: cards[2]), forState: .Normal)
//title = cards[correctAnswer].uppercaseString
}
@IBAction func buttonTapped(sender: UIButton) {
if sender.tag == correctAnswer {
score++
print("correct")
} else {
score--
print("incorrect")
}
}
}
交叉檢查一次圖像名稱與您在卡片數組中指定的名稱相同。句法[0] =靴子不可用 –