只有两个变化:
GeneratorOf现在被称为AnyGenerator.
GeneratorOf.init(next:) 现在是一个功能 anyGenerator()
这给了我们:
class Cars : SequenceType {
var carList : [Car] = []
func generate() -> AnyGenerator<Car> {
// keep the index of the next car in the iteration
var nextIndex = carList.count-1
// Construct a GeneratorOf<Car> instance, passing a closure that returns the next car in the iteration
return anyGenerator {
if (nextIndex < 0) {
return nil
}
return self.carList[nextIndex--]
}
}
}
Run Code Online (Sandbox Code Playgroud)
(我编辑了链接的答案以匹配Swift 2语法.)
| 归档时间: |
|
| 查看次数: |
437 次 |
| 最近记录: |