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 ... –