Swift - 从该类继承的Cast对象

时间:2014-06-09 13:44:43

标签: inheritance casting swift

我收到此错误:无法转换表达式'()'输入$' T5' 从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

1 个答案:

答案 0 :(得分:5)

PlayingCard(...)不是演员。这是一个构造函数调用。

如果你想演员,请使用deck.drawRandomCard() as PlayingCard