今天看到Array 的API中有这么一个声明的函数:
mutating func extend<S : SequenceType where T == T>(newElements: S)
函数名为extend, 所需参数是S类型的newElements, 而S首先要实现SequenceType协议。那SequenceType到底是什么呢?
官方对SequenceType的声明API如下:
protocol SequenceType : _Sequence_Type {
/// A type that provides the *sequence*\ 's iteration interface and
/// encapsulates its iteration state.
typealias Generator : GeneratorType
/// Return a *generator* over the elements of this *sequence*.
///
/// Complexity: O(1)
func generate() -> Generator
}
里面需要实现一个函数generate(),返回了一个GeneratorType类型,GeneratorType又是什么呢?看看APT
protocol GeneratorType {
/// The type of element generated by `self`.
typealias Element
/// Advance to the next element and return it, or `nil` if no next
/// element exists.
///
/// Requires: `next()` has not been applied to a copy of `self`
/// since the copy was made, and no preceding call to `self.next()`
/// has returned `nil`. Specific implementations of this protocol
/// are encouraged to respond to violations of this requirement by
/// calling `preconditionFailure("...")`.
mutating func next() -> Element?
}
GeneratorType必须要实现一个函数next(),它的作用就是返回一个Element,注释里说的很清楚:它的作用就是一直返回元素,直到最后。
具体怎么用呢,咱们从for...in...来了解下:
首先让我们先来看一下常见的这个遍历方法:
for x in mySequence {
// iterations here
}
而Swift实际上是按照下面这个方法来实现的:
var __g: Generator = mySequence.generate()
while let x = __g.next() {
// iterations here
}
这是什么意思呢?
1)Swift调用generate()来生成了一个Generator,这个对象是 一个私有的变量即__g;
2) __g调用了next()函数,返回了一个optional类型对象element?。这个对象element被解包后赋值给了x;
3)通过next()函数一直来获取下一个元素,直到为nil停止。
所以,要实现Sequence必须要先有一个Generator。如下面的例子是对数组进行倒序索引:
1) 先声明继承与GeneratorType的类,实现Element和next()
///GeneratorType
class CountdownGenerator: GeneratorType {
typealias Element = Int
var element: Element
init<T>(array: [T]){
self.element = array.count - 1
}
func next() -> Element? {
return self.element < 0 ? nil : element--
}
}
2)完成继承于SequenceType的类,实现Generator和generate()
class ReverseSequence<T>: SequenceType {
typealias Generator = CountdownGenerator
var array: [T]
init(array: [T]){
self.array = array
}
func generate() -> Generator {
return CountdownGenerator(array: array)
}
}
3)使用:
let array = ["a", "b", "c"]
for i in ReverseSequence(array: array){
println("索引\(i)的值是:\(array[i])")
}
结果:
索引2的值是:c
索引1的值是:b
索引0的值是:a
参考资料:http://segmentfault.com/a/1190000002464158
Generator