相关疑难解决方法(0)

无法在Swift 3的Collection扩展中使用indices.contains()

我在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
    }
}
Run Code Online (Sandbox Code Playgroud)

但事实证明,Swift 3.0没有contains()功能.相反,它为我提供了以下语法:

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

问题是我不知道它在块内应该包含什么.请帮忙迁移它吗?

swift swift3 swift2.3

10
推荐指数
1
解决办法
2365
查看次数

标签 统计

swift ×1

swift2.3 ×1

swift3 ×1