0
我有一個Game
類。我說它是通用的,因爲我需要支持不同類型的電路板。現在我只想添加一個經典的iOS風格的委託,其方法是將一個遊戲和一個新的積分值作爲參數。如何在Swift associatedtype
的方式實現這一點?我很困惑,我無法推動這種簡單的邏輯。在通用類型的委託協議中使用associatedtype
protocol GamePointsDelegate {
associatedtype B: Board
func game(_ game: Game<B>, didSetPoints points: Int)
}
class Game<B: Board> {
let board: Board
var points = 0 {
// Compiler Error
// Member 'game' cannot be used on value of protocol type 'GamePointsDelegate'; use a generic constraint instead
didSet { pointsDelegate?.game(self, didSetPoints: points) }
}
// Compiler Error
// Protocol 'GamePointsDelegate' can only be used as a generic constraint because it has Self or associated type requirements
var pointsDelegate: GamePointsDelegate?
}
刪除相關類型並只使用泛型函數'game'是否可行? – Qbyte
因爲'GamePointsDelegate'不是一個類型,所以你不能有'GamePointsDelegate'類型的'var pointsDelegate'。它就像一個類型的模板,它爲'B'的每個可能值產生一個新類型。 – Alexander
@亞歷山大,我明白了。但是我在'Game'中已經有'B'了,我想以某種方式使用它。 – kelin