gpt4 book ai didi

ios - 不能在类中使用变量

转载 作者:行者123 更新时间:2023-11-28 13:32:17 25 4
gpt4 key购买 nike

<分区>

我正在尝试学习 CS 193P 类(class),我目前正在上第 2 课。讲师制作了 var game = Concentration(numberOfPairsOfCards: cardButtons.count/2) 并且效果很好然而,对他来说,这对我根本不起作用。我错过了什么吗?我不能在那里传递任何东西。

这是我的 ViewController 类:

class ViewController: UIViewController {

@IBOutlet weak var flipCountLabel: UILabel!
@IBOutlet var cardButtons: [UIButton]!

var game = Concentration(numberOfPairsOfCards: cardButtons.count / 2)

var emojiChoices = ["🎃", "👻", "🎃", "👻"]

var flipCount = 0 {
didSet {
flipCountLabel.text = "Flips: \(flipCount)"
}
}

//MARK: - IBActions
@IBAction func touchCard(_ sender: UIButton) {
flipCount += 1
if let cardNumber = cardButtons.index(of: sender) {
flipCard(withEmoji: emojiChoices[cardNumber], on: sender)
}
}


//MARK: - Methods
func flipCard(withEmoji emoji: String, on button: UIButton) {
if button.currentTitle == emoji {
button.setTitle("", for: .normal)
button.backgroundColor = #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1)
} else {
button.setTitle(emoji, for: .normal)
button.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
}
}

}

和浓度:

class Concentration {
var cards = [Card]()

func chooseCard(at index: Int) {

}

init(numberOfPairsOfCards: Int) {
for _ in 1...numberOfPairsOfCards {
let card = Card()
cards += [card, card]
}
//TODO: - Shuffle the cards

}


}

错误是

Cannot use instance member 'cardButtons' within property initializer; property initializers run before 'self' is available

而且自动完成也不起作用

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