gpt4 book ai didi

arrays - swift 发现 fatal error 为零

转载 作者:搜寻专家 更新时间:2023-11-01 07:26:33 26 4
gpt4 key购买 nike

我收到以下错误消息:

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

}
}
}

最佳答案

试试这段代码。它会帮助你。

@IBOutlet weak var btn1: UIButton!
@IBOutlet weak var btn2: UIButton!

var cards = [String] ()

override func viewDidLoad()
{
super.viewDidLoad()
cards += ["11.jpg","1_home.jpg"]
}


@IBAction func btnAction(sender: AnyObject)
{
btn1.setBackgroundImage(UIImage(named: cards[0]), forState: UIControlState.Normal)
btn2.setBackgroundImage(UIImage(named: cards[1]), forState: UIControlState.Normal)
}

关于arrays - swift 发现 fatal error 为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35723261/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com