无法在 Swift 3 的 Collection 扩展中使用 index.contains() [英] Unable to use indices.contains() in a Collection extension in Swift 3

查看:12
本文介绍了无法在 Swift 3 的 Collection 扩展中使用 index.contains()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Swift 2.3 中编写了以下扩展:

I wrote following extension in Swift 2.3:

extension CollectionType {
    /// Returns the element at the specified index iff it is within bounds, otherwise nil.
    subscript (safe index: Index) -> Generator.Element? {
        return indices.contains(index) ? self[index] : nil
    }
}

然而,事实证明 Swift 3.0 没有 contains() 函数.相反,它为我提供了此方法的以下语法:

However, it turns out that Swift 3.0 does not have contains() function. Instead, it offers me following syntax for this method:

indices.contains(where: { (<#Self.Indices.Iterator.Element#>) -> Bool in
    <# code ??? what should it do??? #>
})

问题是我不知道块内应该包含什么.请问有什么关于迁移的帮助吗?

The problem is that I don't know what should it contain inside the block. Any help with migrating it, please?

推荐答案

Swift 4 更新

在 Swift 4 中,感谢 在关联类型上具有 where 子句 的能力,Collection 现在强制执行 IndicesElement 类型与 Collection 类型相同的 Index.

Swift 4 Update

In Swift 4, thanks to the ability to have where clauses on associated types, Collection now enforces that Indices's Element type is the same type as the Collection's Index.

因此这意味着我们可以说:

This therefore means that we can just say:

extension Collection {

    /// Returns the element at the specified index iff it is within bounds, otherwise nil.
    subscript (safe index: Index) -> Element? {
        return indices.contains(index) ? self[index] : nil
    }
}

<小时>

斯威夫特 3

Swift 3 中的 Sequence 协议仍然有一个 contains(_:) 方法,如果序列是 Equatable 元素,则接受序列的元素:


Swift 3

The Sequence protocol in Swift 3 still has a contains(_:) method, which accepts an element of the sequence if the sequence is of Equatable elements:

extension Sequence where Iterator.Element : Equatable {
    // ...
    public func contains(_ element: Self.Iterator.Element) -> Bool
    // ...
}

您遇到的问题是由于Collectionindices 属性要求的类型发生了变化.在 Swift 2 中,它的类型是 Range - 但是在 Swift 3 中,它的类型是 Indices(Collection 协议的关联类型):

The problem you're encountering is due to the change in the type of Collection's indices property requirement. In Swift 2, it was of type Range<Self.Index> – however in Swift 3, it is of type Indices (an associated type of the Collection protocol):

/// A type that can represent the indices that are valid for subscripting the
/// collection, in ascending order.
associatedtype Indices : IndexableBase, Sequence = DefaultIndices<Self>

因为 Collection 协议本身在 Swift 中目前没有办法表达 IndicesIterator.Element 的类型是 Index (Index="https://github.com/apple/swift-evolution/blob/master/proposals/0142-related-types-constraints.md" rel="noreferrer">这将在 Swift 的未来版本中成为可能),编译器无法知道您可以将 Index 类型的内容传递给 contains(_:).这是因为当前完全有可能使类型符合 Collection 并使用 whatever 它想要的元素类型实现 Indices.

As there's currently no way in Swift for the Collection protocol itself to express that Indices's Iterator.Element is of type Index (this will however be possible in a future version of Swift), there's no way for the compiler to know that you can pass something of type Index into contains(_:). This is because it's currently fully possible for a type to conform to Collection and implement Indices with whatever element type it wants.

因此,解决方案是简单地限制您的扩展以确保 Indices does 具有 Index 类型的元素,允许您通过 indexcontains(_:):

Therefore the solution is to simply constrain your extension to ensure that Indices does have elements of type Index, allowing you to pass index into contains(_:):

extension Collection where Indices.Iterator.Element == Index {

    /// Returns the element at the specified index iff it is within bounds, otherwise nil.
    subscript (safe index: Index) -> Iterator.Element? {
        return indices.contains(index) ? self[index] : nil
    }
}

这篇关于无法在 Swift 3 的 Collection 扩展中使用 index.contains()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆