4
我有寫不同的項目泛型參數無法推斷迅速
extension Array {
func distinct<T: Equatable>() -> [T]{
var unique = [T]()
for i in self{
if let item = i as? T {
if !unique.contains(item){
unique.append(item)
}
}
}
return unique
}
}
陣列延伸和嘗試調用這個函數像下面
let words = ["pen", "Book", "pencile", "paper", "Pin", "Colour Pencile", "Marker"]
words.distinct()
但它給錯誤「泛型參數「T '不能被推斷得很快「
看一看http://stackoverflow.com/questions/24091046/unable-to -use-contains-a-swift-array-extension ... –