我在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)
问题是我不知道它在块内应该包含什么.请帮忙迁移它吗?