您的问题来自于使用Collection.Index
关联类型。我不完全确定它是怎么回事,但我认为这是某种内部关联类型,并不符合你的期望。改为使用Collection.Indices.Iterator.Element
。这里是你的代码,修补了
func CheckAnyCollection<C: Collection>(
in c: C,
for desiredElement: C.Iterator.Element
) -> [C.Indices.Iterator.Element]
where C.Iterator.Element: Equatable
{
let matchingIndexElementPairs = zip(c.indices, c)
.lazy
.filter { (index, element) in element == desiredElement }
as LazyFilterSequence<Zip2Sequence<C.Indices, C>>
// annoying type annotation, but necessary to disambiguate
return matchingIndexElementPairs.map{ $0.0 }
}
print(CheckAnyCollection(in: [1, 2, 3, 2, 1], for: 2)) // => [1, 3]
然而,也有相当这段代码的几个问题:
- 斯威夫特方法与lowerCamelCase命名。 UpperCamelCase是为类型保留的。
- 该名称具有误导性。 “CheckAny”根本不检查任何东西。
- 作用于实例的函数应作为扩展中该类型的实例方法实现。
这是我怎么会写这样的代码:
extension Collection where Iterator.Element: Equatable {
func indices(of desiredElement: Iterator.Element) -> [Indices.Iterator.Element] {
let matchingIndexElementPairs = zip(self.indices, self)
.lazy
.filter { (index, element) in element == desiredElement }
as LazyFilterSequence<Zip2Sequence<Self.Indices, Self>>
// annoying type annotation, but necessary to disambiguate
return matchingIndexElementPairs.map{ $0.0 }
}
}
这种方法可以让你有一个更自然的通话网站:
let indices2 = [1, 2, 3, 2, 1].indices(of: 2)
print(indices2) // => [1, 3]
你不需要的方法只为这,这是一个班轮:'拉链(c.indices,C).lazy.filter {$ 0.1 ==元件} .MAP {$ 0.0次}' – Alexander
我试图与字典类型与由 - 亚历山大提供的解决方案。 'VAR的结果= CheckAnyCollection(在:myDictionary,searchFor:3)'**,但得到这个错误**'不能转换int类型的值与预期的参数类型“_._元素”' –
提供一个[最小,完整,可验证示例](https://stackoverflow.com/help/mcve) – Alexander