|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Swift 4 新特性 |
| 4 | +subtitle: 很高兴 Swift 4 不再是一门新语言了😅 |
| 5 | +date: 2017-09-11 |
| 6 | +author: BY |
| 7 | +header-img: img/post-bg-ios10.jpg |
| 8 | +catalog: true |
| 9 | +tags: |
| 10 | + - iOS |
| 11 | + - Swift |
| 12 | +--- |
| 13 | + |
| 14 | + |
| 15 | +### private 权限扩大 |
| 16 | + |
| 17 | +在 Swift 4 中,`extension` 可以读取 `private` 变量了。 |
| 18 | + |
| 19 | +Swift 3 中,如果将主体函数的变量定义为 `private`,则其 `extension` 无法读取此变量,必须将其改为 `filePrivate` 才可以。 |
| 20 | + |
| 21 | +### 单向区间 |
| 22 | + |
| 23 | +单向区间是一个新的类型,主要分两种:确定上限和确定下限的区间。直接用字面量定义大概可以写成 `…6`和 `2…` |
| 24 | + |
| 25 | +例如 |
| 26 | + |
| 27 | +```swift |
| 28 | +let intArr = [0, 1, 2, 3, 4] |
| 29 | + |
| 30 | +let arr1 = intArr[...3] // [0, 1, 2, 3] |
| 31 | +let arr2 = intArr[3...] // [3, 4] |
| 32 | +``` |
| 33 | + |
| 34 | +### 字符串改动 |
| 35 | + |
| 36 | + |
| 37 | +#### String 操作简化了 |
| 38 | + |
| 39 | +`String` 许多要通过 `.characters` 进行的操作,可以直接用 String 进行操作了。 |
| 40 | + |
| 41 | +例如: |
| 42 | + |
| 43 | +```swift |
| 44 | +let greeting = "Hello, 😜!" |
| 45 | +// No need to drill down to .characters |
| 46 | +let n = greeting.count |
| 47 | +let endOfSentence = greeting.index(of: "!")! |
| 48 | + |
| 49 | +``` |
| 50 | + |
| 51 | +#### 新增 Substring 类型 |
| 52 | + |
| 53 | + |
| 54 | +swift 4 为字符串片段新增了一个叫 `Substring` 的类型。 |
| 55 | + |
| 56 | +当你创建一个字符串的片段时,会产生一个 `Substring` 实例。`Substring` 与 `String` 用法相同, 因为子串和原字符串共享内存,所以对子串的操作快速而且高效。 |
| 57 | + |
| 58 | +```swift |
| 59 | +let greeting = "Hi there! It's nice to meet you! 👋" |
| 60 | +let endOfSentence = greeting.index(of: "!")! |
| 61 | + |
| 62 | +// 产生 Substring 实例 |
| 63 | +let firstSentence = greeting[...endOfSentence] |
| 64 | +// firstSentence == "Hi there!" |
| 65 | + |
| 66 | +// `Substring` 与 `String` 用法相同 |
| 67 | +let shoutingSentence = firstSentence.uppercased() |
| 68 | +// shoutingSentence == "HI THERE!" |
| 69 | +``` |
| 70 | + |
| 71 | +但是要注意一个 `Substring` 保留从其生成的完整的 `String`值。 当您传递一个看似很小的 `Substring` 时,这可能导致意外的高内存开销。所以使用 `Substring`时,最好转化为 `String`. |
| 72 | + |
| 73 | +```swift |
| 74 | +let newString = String(substring) |
| 75 | +``` |
| 76 | + |
| 77 | + |
| 78 | +#### 换行可以不用 `\n`了! |
| 79 | + |
| 80 | +Swift 3,字符串换行要插入 `\n`。 |
| 81 | +例如: |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +在 Swift 4 可以这样操作: |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +用两个 `“”“` 包裹起来的字符串会自动添加 `\n` 换行,更加直观了。注意:换行与缩进参照的是第二个 `“”“` 号的位置。 |
| 90 | + |
| 91 | +嗯,我觉得OK! |
| 92 | + |
| 93 | +#### 支持 Unicode 9 |
| 94 | + |
| 95 | +Swift 4 支持 Unicode 9,[为现代表情符号修正了一些问题](https://oleb.net/blog/2016/12/emoji-4-0/)。 |
| 96 | + |
| 97 | + |
| 98 | +```swift |
| 99 | +let family1 = "👨👩👧👦" |
| 100 | +let family2 = "👨\u{200D}👩\u{200D}👧\u{200D}👦" |
| 101 | +family1 == family2 // → true |
| 102 | +``` |
| 103 | + |
| 104 | +居然还有这种操作~ |
| 105 | + |
| 106 | +### 新增 KeyPath 数据类型 |
| 107 | + |
| 108 | +KeyPath 是 Swift 4 新增加的数据类型。 |
| 109 | + |
| 110 | +定义两个结构体 `Person`与`Book` |
| 111 | + |
| 112 | +```swift |
| 113 | +struct Person { |
| 114 | + var name: String |
| 115 | +} |
| 116 | + |
| 117 | +struct Book { |
| 118 | + var title: String |
| 119 | + var authors: [Person] |
| 120 | + var primaryAuthor: Person { |
| 121 | + return authors.first! |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +let abelson = Person(name: "Harold Abelson") |
| 126 | +let sussman = Person(name: "Gerald Jay Sussman") |
| 127 | +let book = Book(title: "Structure and Interpretation of Computer Programs", authors: [abelson, sussman]) |
| 128 | +``` |
| 129 | +```swift |
| 130 | +book[keyPath: \Book.title] |
| 131 | +book[keyPath: \Book.primaryAuthor.name] |
| 132 | +// 相当与 |
| 133 | +book.title |
| 134 | +book.primaryAuthor.name |
| 135 | +``` |
| 136 | + |
| 137 | +这里 `\Book.title` 与 `\Book.primaryAuthor.name` 就是 KeyPath. |
| 138 | + |
| 139 | +KeyPath 可以用 `.appending` 拼接 |
| 140 | + |
| 141 | +```swift |
| 142 | +let authorKeyPath = \Book.primaryAuthor |
| 143 | +let nameKeyPath = authorKeyPath.appending(path: \.name) |
| 144 | +// nameKeyPath = \Book.primaryAuthor.name |
| 145 | +``` |
| 146 | + |
| 147 | +### 新增 `swapAt()` 函数 |
| 148 | +Swift 4 引入了一种在集合中交换两个元素的新方法: `swapAt()` |
| 149 | + |
| 150 | +Swift 3 交换集合中的元素的用 `swap()` |
| 151 | + |
| 152 | +```swift |
| 153 | +var numbers = [1,2,3,4,5] |
| 154 | +swap(&numbers[0], &numbers[1]) |
| 155 | +// numbers = [2,1,3,4,5] |
| 156 | +``` |
| 157 | + |
| 158 | +Swift 4 中可以直接用 |
| 159 | + |
| 160 | +```swift |
| 161 | +var numbers = [1,2,3,4,5] |
| 162 | +numbers.swapAt(0,1) |
| 163 | +// numbers = [2,1,3,4,5] |
| 164 | +``` |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | +### 其他改动 |
| 169 | + |
| 170 | +其他改动如:**新的整数协议**、**泛型下标**、**NSNumber bridging**等 |
| 171 | + |
| 172 | +可以参考:[whats new in swift4](https://github.com/ole/whats-new-in-swift-4) |
0 commit comments