Viv*_*ive 10 swift swift3 swift2.3
我在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)
问题是我不知道它在块内应该包含什么.请帮忙迁移它吗?
Ham*_*ish 27
在夫特4,由于能力具有where对相关联的类型的条款,Collection现在强制执行Indices的Element类型是相同的类型Collection的Index.
因此,这意味着我们可以说:
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
}
}
Run Code Online (Sandbox Code Playgroud)
SequenceSwift 3中的协议仍然有一个contains(_:)方法,如果序列是Equatable元素,它接受序列的一个元素:
extension Sequence where Iterator.Element : Equatable {
// ...
public func contains(_ element: Self.Iterator.Element) -> Bool
// ...
}
Run Code Online (Sandbox Code Playgroud)
您遇到的问题是由于类型的变化Collection的indices性能要求.在Swift 2中,它是类型的Range<Self.Index>- 但是在Swift 3中,它是类型Indices(Collection协议的相关类型):
/// A type that can represent the indices that are valid for subscripting the
/// collection, in ascending order.
associatedtype Indices : IndexableBase, Sequence = DefaultIndices<Self>
Run Code Online (Sandbox Code Playgroud)
由于有目前在斯威夫特没有办法的Collection协议本身来表达Indices的Iterator.Element是类型的Index(然而,这将有可能在斯威夫特的未来版本),有没有办法让编译器知道你可以通过类型的东西Index进入contains(_:).这是因为目前完全有可能的类型,以符合Collection并实现Indices与任何元素类型就是了.
因此,解决方案是简单地限制你的扩展,以确保Indices 不会有类型的元素Index,让你传递index到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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2365 次 |
| 最近记录: |