Swift4.0 新特性----Swift标准库

来源:互联网 发布:java判断白天还是晚上 编辑:程序博客网 时间:2024/06/02 02:16

Encoding and Decoding

当需要将一个对象持久化时,需要把这个对象序列化,往常的做法是实现 NSCoding 协议,写过的人应该都知道实现 NSCoding 协议的代码写起来很痛苦,尤其是当属性非常多的时候。几年前有一个工具能自动生成 Objective-C 的实现 NSCoding 协议代码,当时用着还不错,但后来这个工具已经没有人维护很久了,而且不支持 Swift。
Swift 4 中引入了 Codable 帮我们解决了这个问题。

struct Language: Codable {    var name: String    var version: Int}

我们想将这个 Language 对象的实例持久化,只需要让 Language 符合 Codable 协议即可,Language 中不用写别的代码。符合了 Codable 协议以后,可以选择把对象 encode 成 JSON 或者 PropertyList。
Encode 操作如下:

let swift = Language(name: "Swift", version: 4)if let encoded = try? JSONEncoder().encode(swift) {    // 把 encoded 保存起来}

Decode 操作如下:

if let decoded = try? JSONDecoder().decode(Language.self, from: encoded) {    print(decoded.name)}


Sequence 改进


Swift 3:protocol Sequence {    associatedtype Iterator: IteratorProtocol    func makeIterator() -> Iterator}Swift 4:protocol Sequence {    associatedtype Element    associatedtype Iterator: IteratorProtocol where Iterator.Element == Element    func makeIterator() -> Iterator}

由于 Swift 4 中的 associatedtype 支持追加 where 语句,所以 Sequence 做了这样的改进。
Swift 4 中获取 Sequence 的元素类型可以不用 Iterator.Element,而是直接取 Element。
SubSequence 也做了修改:

protocol Sequence {    associatedtype SubSequence: Sequence         where SubSequence.SubSequence == SubSequence,              SubSequence.Element == Element}


通过 where 语句的限定,保证了类型正确,避免在使用 Sequence 时做一些不必要的类型判断。
Collection 也有一些类似的修改。


Protocol-oriented integers

整数类型符合的协议有修改,新增了 FixedWidthInteger 等协议,具体的协议继承关系如下:

                +-------------+   +-------------+        +------>+   Numeric   |   | Comparable  |        |       |   (+,-,*)   |   | (==,<,>,...)|        |       +------------++   +---+---------+        |                     ^       ^+-------+------------+        |       ||    SignedNumeric   |      +-+-------+-----------+|     (unary -)      |      |    BinaryInteger    |+------+-------------+      |(words,%,bitwise,...)|       ^                    ++---+-----+----------+       |         +-----------^   ^     ^---------------+       |         |               |                     |+------+---------++    +---------+---------------+  +--+----------------+|  SignedInteger  |    |  FixedWidthInteger      |  |  UnsignedInteger  ||                 |    |(endianness,overflow,...)|  |                   |+---------------+-+    +-+--------------------+--+  +-+-----------------+                ^        ^                    ^       ^                |        |                    |       |                |        |                    |       |               ++--------+-+                +-+-------+-+               |Int family |-+              |UInt family|-+               +-----------+ |              +-----------+ |                 +-----------+                +-----------+



Dictionary and Set enhancements


这里简单列一下 Dictionary 和 Set 增强了哪些功能:
通过 Sequence 来初始化
可以包含重复的 Key
Filter 的结果的类型和原类型一致
Dictionary 的 mapValues 方法
Dictionary 的默认值
Dictionary 可以分组
Dictionary 可以翻转


NSNumber bridging and Numeric types

在 Swift 4 中,把一个值为 999 的 NSNumber 转换为 UInt8 后,能正确的返回 nil,而在 Swift 3 中会不可预料的返回 231。

let n = NSNumber(value: 999)let v = n as? UInt8 // Swift 4: nil, Swift 3: 231


MutableCollection.swapAt(::)

MutableCollection 现在有了一个新方法 swapAt(::) 用来交换两个位置的值,例如:

var mutableArray = [1, 2, 3, 4]mutableArray.swapAt(1, 2)print(mutableArray)// 打印结果:[1, 3, 2, 4]