能夠?qū)⒆兞康念愋头盒图s束化嗎?
extension?Record{ ????public?static?func?<(lhs:?Self,?rhs:?Self)?->?Bool{ ????????return?lhs.winningPercent()?<?rhs.winningPercent() ????} ????public?static?func?==(lhs:?Self,?rhs:?Self)?->?Bool{ ????????return?lhs.winningPercent()?==?rhs.winningPercent() ????} }
用上一視頻使用的例子,創(chuàng)建了三個(gè)遵守Record這個(gè)協(xié)議的結(jié)構(gòu)體(BaseballRec, BasketballRec, FootballRec)
其三者的實(shí)例(baseTeamRec,basketTeamRec,footTeamRec)是可以兩兩互相比較的,也可以調(diào)用isPrizable()方法,但當(dāng)帶入函數(shù)topPrizable時(shí)
func topPrizable<T: Record & Prizable>(list: [T]) -> T?
topPrizable(list: [baseTeamRec, basketTeamRec, footTeamRec] )
會(huì)提示類型不匹配的錯(cuò)誤,要怎麼修正呢?
Cannot convert value of type '[Any]' to expected argument type '[_]'
且要將這些實(shí)例存成Record的Array也有錯(cuò)誤,提示Record Protocol 要成爲(wèi)類型得要用泛型約束,gameRecords的類型要如何定義呢?
let gameRecords: [Record] =?[baseTeamRec, basketTeamRec, footTeamRec]
Protocol 'Record' can only be used as a generic constraint because it has Self or associated type requirements
2016-09-17
漏了一個(gè)關(guān)鍵字,應(yīng)該是這樣:func topPrizable<T: protocol<Record, Prizable>>(list: [T]) -> T?
2016-09-26
請(qǐng) 劉老師 ?出來 解答一下。。。。。。。。。。。。等待
2016-09-17
可以使用聚合協(xié)議來定義,就是這樣子func topPrizable<T: <Record, Prizable>>(list: [T]) -> T?