【发布时间】:2020-02-12 12:59:46
【问题描述】:
我的目标是通过所有Sequence 提供一个函数,例如数组或集合。
这个函数应该返回一个Bool,告诉两个序列中是否至少有一个对象。
// Usage
let s1 = ["hi", "hey", "ho"]
let s2:Set = ["woop", "oi", "yes"]
if s2.intersects(with: s1) {
print("Happy me, it's a match")
}
extension Sequence where Element:Equatable {
func intersects<T:Sequence>(with anotherSequence:T) -> Bool
where T.Element: Equatable {
// ⬇ error: `Extraneous argument label 'where:' in call`
return self.contains(where: anotherSequence.contains)
}
}
// doing the same function outside works:
let rez = s1.contains(where: s2.contains)
print(rez)
我觉得我快到了,但我不明白为什么第一个 contains(where:) 会给我这个错误。
contains() 和 contains(where:) 都属于 Sequence 不?
我错过了什么?
【问题讨论】:
标签: swift sequence swift-extensions