atSwift 2016 笔记(三)Swift Style


2016 atSwift - Swift Style by Greg Heo

  • Info:
    • Swift 3.0
    • Xcode 8.2.1
    • macOS 10.12.4 beta (16E175b)

前言

atSwift 2016 是 2016 年国内举行的一场 Swift 开发者大会。虽然已经过去一年多,但其中还是有很多值得学习的地方。所以这次的笔记就针对其对外公开的视频及资料,学习一下前沿开发者的思路思维。当然,由于当时 Swift 的大版本为 2,因此本文会针对其中的代码进行更新为 3.0。

由于该节中提到了 WWDC15-411,关于其内容请参考 WWDC 2015.411 - Swift in Practice 笔记

Value Types

enum

  • 一个枚举的多个 case 的原始值(raw value)唯一,编译器会进行检查。
  • 枚举使用场景:
    • 已知的一系列值(Well-defined set of values)
    • 转场(Segues)
    • Asset catalog 项(Asset catalog items)

Protocol

  • 本节主要介绍了几个协议,详见以下 Demo 及注释。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
struct Sentence {
let sentence: String
init(_ sentence: String) {
self.sentence = sentence
}
}
let mySentence = Sentence("maimieng.com")
// 扩展比较运算符
extension Sentence : Equatable {
static func ==(lhs: Sentence, rhs: Sentence) -> Bool {
return lhs.sentence == rhs.sentence
}
static func <(lhs: Sentence, rhs: Sentence) -> Bool {
return lhs.sentence < rhs.sentence
}
static func >(lhs: Sentence, rhs: Sentence) -> Bool {
return lhs.sentence > rhs.sentence
}
static func >=(lhs: Sentence, rhs: Sentence) -> Bool {
return lhs.sentence >= rhs.sentence
}
}
mySentence == Sentence("maimieng.com")
mySentence != Sentence("github.com/kingcos")
mySentence > Sentence("maimieng.com")
mySentence < Sentence("maimieng.com")
mySentence >= Sentence("maimieng.com")
// 扩展自定义转换字符串 Sentence(sentence: "") -> ""
extension Sentence : CustomStringConvertible {
var description: String {
return sentence
}
}
print(mySentence)
// 扩展使用字符串字面量(Literal)初始化
// let mySentence = Sentence("maimieng.com")
// -> let mySentence: Sentence = "maimieng.com"
extension Sentence : ExpressibleByStringLiteral {
init(stringLiteral value: String) {
self.init(value)
}
init(unicodeScalarLiteral value: String) {
self.init(value)
}
init(extendedGraphemeClusterLiteral value: String) {
self.init(value)
}
}
let newSentence: Sentence = "maimieng.com"
// 扩展哈希化
// var sentenceDict = Dictionary<Sentence, String>() -> Allow
extension Sentence : Hashable {
var hashValue: Int {
return sentence.hashValue
}
}
var sentenceDict = Dictionary<Sentence, String>()
sentenceDict[mySentence] = "My website"
// 扩展序列化,支持 for-in 循环
extension Sentence: Sequence {
func makeIterator() -> SentenceGenerator {
let words = sentence.components(separatedBy: ".")
return SentenceGenerator(words: words)
}
}
struct SentenceGenerator: IteratorProtocol {
let words: [String]
var index = 0
init(words: [String]) {
self.words = words
}
mutating func next() -> String? {
if index < words.count {
let thisIndex = index
index += 1
return words[thisIndex]
} else {
return nil
}
}
}
for word in mySentence {
print(word)
}
// OUTPUT:
// maimieng.com
// maimieng
// com

Safety

1
2
3
4
let i = 1
// precondition(i == 0, "i 不为 0")
// assert(i == 0, "i 不为 0")
// fatalError("ERROR")
  • Preconditions
    • 前置条件不满足时,会同时影响 Debug 和 Release 阶段。
  • Assertions
    • 断言检查只活跃于 Debug 阶段,不会影响性能。
  • fatalError()
    • 无条件终止运行。

参考

才疏学浅,如有错误或不当之处,请您一定指出! 如果能帮到您,那就再好不过了~