gpt4 book ai didi

inheritance - Swift - 从该类继承的 Cast 对象

转载 作者:搜寻专家 更新时间:2023-11-01 06:10:43 25 4
gpt4 key购买 nike

我收到此错误:无法将表达式的类型 '()' 转换为类型 $'T5'PlayingCard 继承自 Card,我知道 drawRandomCard 返回一个 PlayingCard。我怎样才能进行这种转换?

var deck = PlayingCardDeck()
var card = PlayingCard()

class ViewController: UIViewController {



override func viewDidLoad() {
super.viewDidLoad()

}

@IBAction func cardTouchBtn(sender : UIButton) {

if !sender.currentTitle{sender.setTitle("", forState: UIControlState.Normal)}
if sender.currentTitle.isEmpty{
card = PlayingCard(deck.drawRandomCard())
sender.setBackgroundImage(UIImage(named : "cardFront"), forState: UIControlState.Normal)
sender.setTitle(card.contents, forState: UIControlState.Normal)
}else{
sender.setBackgroundImage(UIImage(named : "cardBack"), forState: UIControlState.Normal)
sender.setTitle("", forState: UIControlState.Normal)
}
}

}

git 上的完整代码:https://github.com/victoryudi/CS193p-Swift/tree/master/FlipCard

最佳答案

PlayingCard(...) 不是类型转换。这是一个构造函数调用。

如果你想转换,使用deck.drawRandomCard() as PlayingCard

关于inheritance - Swift - 从该类继承的 Cast 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24121612/

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