我编写了这个函数来获取两个字符串数组之间的差异。
func difference<T:Hashable>(array1: [T] ,array2:[T]) ->[T]? {
let set1 = Set<T>(array1)
let set2 = Set<T>(array2)
let intersection = set1.symmetricDifference(set2)
return Array(intersection)
}
现在我想将它扩展到不同类型的通用函数,比如Int
等Double
......
extension Array where Element: Hashable {
func difference<T:Hashable>(array2: [T]) -> [T] {
let set1 = Set(self)
let set2 = Set(array2)
let intersection = set1.symmetricDifference(set2)
return Array(intersection)
}
}
使用此扩展程序,我收到错误:
Generic parameter 'S' could not be inferred.
我尝试了不同的方法但徒劳无功。可能是什么问题呢?