2014-06-09 120 views
1

我收到此錯誤:無法將表達式的類型'()'轉換爲鍵入$'T5' Card從CardCard繼承,並且我知道drawRandomCard返回了一個PlayingCard。 如何進行此轉換?Swift - 繼承自該類的Cast對象

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

回答

5

PlayingCard(...)不是一個演員。這是一個構造函數調用。

如果要投射,請使用deck.drawRandomCard() as PlayingCard

+0

不能相信我錯過了。謝謝! – vyudi