Swift语法——Swift Sequences 探究
来源:互联网 发布:java工程师要学什么 编辑:程序博客网 时间:2024/05/22 05:26
今天看到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()
///GeneratorTypeclass 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
0 0
- Swift语法——Swift Sequences 探究
- 2、swift开发iOS——swift语法
- Swift语法—— where语句使用
- Swift学习笔记——基础语法
- Swift开发基础篇—基本语法
- Swift学习Day02(基础语法—)
- swift语法
- Swift语法
- Swift语法
- Swift 语法
- Swift语法
- Swift:Generators 与 Sequences 浅析
- Swift:Generators 与 Sequences 浅析
- Swift初次探究
- Swift 构造器探究
- swift之extension探究
- Swift语法00.Swift 介绍
- iOS开发swift语法初级篇—————(swift技术交流群:361513739)
- C#简单生成订单编号--通过精确到毫秒的时间格式
- linux sigaction函数(注册信号)使用
- 论文提要“Pedestrian Detection aided by Deep Learning Semantic Tasks”
- 三度怀孕的湖南14岁年轻少女妈妈(一)
- 使用QtService接口的时候出现undefined reference to `::staticMetaObject'
- Swift语法——Swift Sequences 探究
- Spring 传统AOP配置
- machine learning model 简单罗列
- Target-Action
- 如何使用ThreadingTest提高软件安全性检测效率(上)
- 一些专业学习的文档保存
- android与javascript的交互
- 【iOS开发】---- SDWebImage支持webp格式的图片
- RestTemplate 电脑版精简客户端