Swift 集合上的扩展和MutableCollection冲突,并被Xcode声明为冗余

Swift 集合上的扩展和MutableCollection冲突,并被Xcode声明为冗余,swift,swift4,Swift,Swift4,我有一个文件,其中有两个不同的扩展名: extension MutableCollection where Indices.Iterator.Element == Index { } extension Collection where Indices.Iterator.Element == Index {} 在可变类中,我只有可变的扩展(有意义!对吗?),而在集合中,我有两个类通用的扩展 问题是我从Swift编译器得到了以下警告: 冗余同类型约束“Self.Index”== 'Self.in

我有一个文件,其中有两个不同的扩展名:

extension MutableCollection where Indices.Iterator.Element == Index { }
extension Collection where Indices.Iterator.Element == Index {}
在可变类中,我只有可变的扩展(有意义!对吗?),而在集合中,我有两个类通用的扩展

问题是我从Swift编译器得到了以下警告:

冗余同类型约束“Self.Index”== 'Self.index.Iterator.Element'


我该怎么做才能修复它呢?

问题不是两个扩展冲突,而是 相同的警告只包含一个扩展名

extension Collection where Indices.Iterator.Element == Index { }
简短的回答是:只需从声明中删除多余的约束:

extension MutableCollection { ... }
extension Collection { ... }
解释:Swift 3不允许约束关联的 类型。即使满足了
Index.Iterator.Element==Index
对于所有具体集合(数组、ArraySlice、集合,…) 语言不需要它。这就是为什么你必须加上 如果需要,请将此约束添加到扩展

Swift 4现在允许使用
where
第条,见

集合
协议定义

associatedtype Indices : Sequence = DefaultIndices<Self>
         where Self.Index == Self.Indices.Element, ...
associatedtype Element where Self.Element == Self.Iterator.Element
所以我们总是有身份

Indices.Iterator.Element == Indices.Element == Index
因此,您可以从代码中删除这些约束