內容簡介
全書分為7章,第1章介紹了與iOS開發環境搭建有關的知識點。第2章是Swift基礎語法,基於Swift 1.2正式版本。第3章是Swift進階語法,除了有難度的語法點之外,還加入了Swift 2.0正式版本的語法改動與新特性。第4章是作者根據史丹福大學iOS 8公開課的計算器項目進行改進的計算器項目,通過這個簡單的項目介紹了Xcode的基本用法和在iOS開發中的重要概念——MVC模式。第5章介紹了iOS中的重要內容UIKit框架,涵蓋了UIKit中常用的控制項,並通過豐富的實例展示了每一個控制項的用法,第5章的最後幾節介紹了iOS 9中的新成員,並詳細講解了何如使用AutoLayout對頁面元素進行布局。第6章介紹了iOS系統API的用法,全部基於Xcode 7.0正式版,使用Swift 2.0語法。第7章是作者精心挑選的一些幫助貼士。
圖書目錄
目錄
第1章 搭建Swift開發環境 ........................................................................................................... 1
1.1 Swift 語言介紹 .......................................................................................................................... 1
1.1.3 Swift 語言的前世今生 .................................................................................................. 1
1.1.2 Swift 與Objective - C ................................................................................................... 2
1.2 Mac OS X 作業系統 .................................................................................................................. 3
1.3 Xcode 簡介和獲取方法 ............................................................................................................ 4
1.3.1 Xcode 簡介 .................................................................................................................... 4
1.3.2 Playground 簡介 ............................................................................................................ 4
1.3.3 Xcode 的獲取方法 ........................................................................................................ 5
1.4 iphone SDK 簡介 ....................................................................................................................... 6
第2章 Swift基礎語法 ................................................................................................................... 7
2.1 基礎知識................................................................................................................................... 7
2.1.1 命名規則 ....................................................................................................................... 7
2.1.2 常量與變數 ................................................................................................................... 8
2.1.3 類型推測 ....................................................................................................................... 9
Swift 開發手冊:技巧與實戰
II
2.1.4 注釋 ............................................................................................................................. 10
2.1.5 輸出常量和變數 ......................................................................................................... 10
2.2 基本數據類型 .......................................................................................................................... 11
2.2.1 整數 ............................................................................................................................. 11
2.2.2 浮點數 ......................................................................................................................... 11
2.2.4 元組類型 ..................................................................................................................... 12
2.2.5 可選型 ......................................................................................................................... 13
2.3 基本運算符 .............................................................................................................................. 15
2.3.1 賦值運算符 ................................................................................................................. 15
2.3.2 數值運算 ..................................................................................................................... 15
2.3.3 自增和自減運算 ......................................................................................................... 16
2.3.4 複合賦值 ..................................................................................................................... 16
2.3.5 比較運算 ..................................................................................................................... 16
2.3.6 三元運算符 ................................................................................................................. 16
2.3.7 邏輯運算符 ................................................................................................................. 17
2.3.8 範圍 ............................................................................................................................. 17
2.3.9 括弧優先權 ................................................................................................................. 18
2.4 字元串與字元 .......................................................................................................................... 18
2.5 集合類型................................................................................................................................. 22
2.5.1 數組 ............................................................................................................................. 22
2.5.2 集合 ............................................................................................................................. 25
2.5.3 字典 ............................................................................................................................. 26
2.6 控制流 .................................................................................................................................... 27
2.6.1 For 循環 ....................................................................................................................... 27
2.6.2 while 循環 .................................................................................................................... 28
2.6.3 If 判斷語句 .................................................................................................................. 29
2.6.4 Switch 開關語句 ......................................................................................................... 29
2.7 函式 ........................................................................................................................................ 31
2.7 閉包 ........................................................................................................................................ 34
2.9 Swift 三傑——類、結構體、枚舉 ........................................................................................ 37
2.9.1 Swift 三傑簡介 ............................................................................................................ 37
2.9.2 值引用與類型引用 ...................................................................................................... 38
2.9.3 類 ................................................................................................................................ 38
2.9.4 結構體 ......................................................................................................................... 39
目錄
III
2.9.5 枚舉 ............................................................................................................................. 40
2.10 屬性 ........................................................................................................................................ 41
2.10.1 存儲屬性 ................................................................................................................... 41
2.10.2 計算屬性 ................................................................................................................... 42
2.10.3 屬性觀察器 ............................................................................................................... 44
2.10.4 類型屬性 ................................................................................................................... 45
2.11 方法 ........................................................................................................................................ 46
2.12 下標 ........................................................................................................................................ 49
2.13 繼承 ........................................................................................................................................ 51
2.14 構造與析構 .............................................................................................................................. 53
2.14.1 構造器 ....................................................................................................................... 54
2.14.2 析構器 ....................................................................................................................... 57
2.15 類型檢查與類型轉換 .............................................................................................................. 58
2.15.1 類型檢查 ................................................................................................................... 59
2.15.2 類型轉換 ................................................................................................................... 60
2.16 類型嵌套................................................................................................................................. 61
2.17 擴展 ........................................................................................................................................ 62
2.17.1 擴展計算屬性 ........................................................................................................... 62
2.17.2 擴展構造器 ............................................................................................................... 63
2.17.3 擴展方法 ................................................................................................................... 63
2.17.4 擴展下標 ................................................................................................................... 64
2.18 協定 ........................................................................................................................................ 64
2.18.1 聲明協定 ................................................................................................................... 64
2.18.2 遵守協定 ................................................................................................................... 66
2.18.3 實現協定 ................................................................................................................... 66
2.19 泛型 ........................................................................................................................................ 66
2.20 斷言 ........................................................................................................................................ 67
第3章 Swit進階語法 .................................................................................................................. 69
3.1 再談可選型 .............................................................................................................................. 69
3.1.1 可選型 ......................................................................................................................... 69
3.1.2 為什麼要用可選型 ...................................................................................................... 70
3.1.3 解包可選型 ................................................................................................................. 71
Swift 開發手冊:技巧與實戰
IV
3.1.4 可選綁定 ..................................................................................................................... 72
3.1.5 可選鏈 ......................................................................................................................... 73
3.2 AnyObject ............................................................................................................................... 74
3.2.1 AnyObject 簡介 ........................................................................................................... 74
3.2.2 AnyObject 的使用 ....................................................................................................... 74
3.3 幾個數組相關的實用函式 ...................................................................................................... 76
3.3.1 filter 函式 ..................................................................................................................... 76
3.3.2 map 函式...................................................................................................................... 76
3.3.3 reduce 函式 .................................................................................................................. 78
3.3.4 sort 函式 ...................................................................................................................... 78
3.3.5 contains 函式 ............................................................................................................... 78
3.3.6 find 函式 ...................................................................................................................... 78
3.4 Objective – C 兼容性 .............................................................................................................. 79
3.5 ARC 自動引用計數 ................................................................................................................ 80
3.5.1 ARC 原理 .................................................................................................................... 80
3.5.2 循環強引用 ................................................................................................................. 82
3.5.3 弱引用與無主引用 ...................................................................................................... 83
3.6 帶下標的遍歷 .......................................................................................................................... 84
3.6.1 C 語言風格的for 循環 ............................................................................................... 84
3.6.2 快速遍歷 ..................................................................................................................... 84
3.6.3 enumerateObjectsUsingBlock ...................................................................................... 85
3.6.4 enumerate ..................................................................................................................... 85
3.7 方法參數的語法甜頭 .............................................................................................................. 85
3.7.1 可變參數 ..................................................................................................................... 85
3.7.2 柯里化 ......................................................................................................................... 86
3.8 閉包的“延遲調用”特性 ...................................................................................................... 88
3.9 Swift 2.0 新特性詳解 .............................................................................................................. 90
3.9.1 println 簡化為print ..................................................................................................... 90
3.9.2 do-while 更名為repeat-while ..................................................................................... 91
3.9.3 where 關鍵字 ............................................................................................................... 91
3.9.4 if-case 結構 .................................................................................................................. 91
3.9.5 guard 關鍵字 ............................................................................................................... 92
3.9.6 錯誤處理 ..................................................................................................................... 93
3.9.7 LLVM 與泛型特化 ..................................................................................................... 96
目錄
V
3.9.8 Protocol Extensions ..................................................................................................... 97
3.9.9 API 的可用性檢查 ...................................................................................................... 98
3.9.10 String 與NSString 言歸於好 .................................................................................... 99
3.9.11 總結 ......................................................................................................................... 100
第4章 IOS開發中的MVC模式 ................................................................................................. 101
4.1 IOS 系統初探 ........................................................................................................................ 101
4.1.1 核心OS 層 ................................................................................................................ 102
4.1.2 核心服務層 ............................................................................................................... 102
4.1.3 媒體層 ....................................................................................................................... 103
4.1.4 Cocoa Touch .............................................................................................................. 103
4.2 MVC 模式 ............................................................................................................................. 103
4.2.1 MVC 簡介 ................................................................................................................. 103
4.2.2 IOS 中的MVC .......................................................................................................... 103
4.3 新建一個Swift 工程 ............................................................................................................. 106
4.4 認識Interface Builder ........................................................................................................... 110
4.5 構建計算器界面 .................................................................................................................... 113
4.5.1 使用對象庫中的對象 ................................................................................................ 114
4.5.2 使用檢查器設定對象 ................................................................................................ 115
4.5.3 嘗試運行程式 ........................................................................................................... 116
4.5.4 添加約束 ................................................................................................................... 118
4.5.5 關聯代碼 ................................................................................................................... 121
4.5.6 完善按鍵 ..................................................................................................................... 126
4.6 實現計算器邏輯 .................................................................................................................... 127
4.6.1 補全鍵盤 ..................................................................................................................... 128
4.6.2 給鍵盤添加約束 ......................................................................................................... 129
4.6.3 實現數字顯示功能...................................................................................................... 132
4.6.4 實現運算邏輯 ............................................................................................................. 134
4.7 修改計算器為MVC 模式 ..................................................................................................... 136
4.8 NSNotification ....................................................................................................................... 138
4.8.1 NSNotification 簡介 .................................................................................................. 138
4.8.2 addObserver 方法 ...................................................................................................... 139
4.8.3 addObserverForName 方法 ....................................................................................... 142
Swift 開發手冊:技巧與實戰
VI
4.8.4 postNotification 方法 ................................................................................................. 143
第5章 掌控UIKit ...................................................................................................................... 144
5.1 本地化 .................................................................................................................................. 144
5.2 視圖View .............................................................................................................................. 146
5.3 生命周期............................................................................................................................... 150
5.3.1 APP 的生命周期 ....................................................................................................... 150
5.3.2 控制器的生命周期 .................................................................................................... 151
5.4 Button(按鈕) ..................................................................................................................... 152
5.4.1 按鈕屬性檢查器 ....................................................................................................... 153
5.4.2 按鈕的代碼實現 ....................................................................................................... 154
5.4.3 為按鈕添加事件 ....................................................................................................... 156
5.5 Label(標籤) ...................................................................................................................... 157
5.5.1 Label 的屬性檢查器 ................................................................................................. 158
5.5.2 Label 的代碼實現 ..................................................................................................... 159
5.6 TextField(文本框) ............................................................................................................ 160
5.6.1 TextField 的屬性檢查器 ........................................................................................... 161
5.6.2 TextField 的代碼實現 ............................................................................................... 163
5.6.3 鍵盤 ........................................................................................................................... 164
5.7 Switch(開關) .................................................................................................................... 164
5.8 Segmented Control(分段控制項) ......................................................................................... 166
5.5.1 Segmented Control 的屬性檢查器 ............................................................................ 167
5.5.2 Segmented Control 的代碼實現 ................................................................................ 168
5.5.3 “按鈕”還是“分段” ............................................................................................ 169
5.9 Slider(滑塊) ...................................................................................................................... 170
5.9.1 Slider 的屬性觀察器 ................................................................................................. 170
5.9.2 Slider 的代碼實現 ..................................................................................................... 171
5.10 ProgressView&ActivityIndicatorView ................................................................................ 172
5.10.1 多執行緒入門 ............................................................................................................. 173
5.10.2 ProgressView ........................................................................................................... 174
5.10.3 ActivityIndicatorView(環形進度條) .................................................................. 176
5.11 Stepper(步進器) ............................................................................................................. 179
5.11.1 Stepper 屬性檢查器 ................................................................................................ 179
目錄
VII
5.11.2 Stepper 的代碼實現 ................................................................................................ 180
5.12 ImageView(圖像控制項) ................................................................................................... 181
5.12.1 UIImageView&UIImage ......................................................................................... 181
5.12.2 UIImageView 屬性檢查器 ...................................................................................... 183
5.12.3 UIImageView 的代碼實現 ...................................................................................... 184
5.13 PickerView&DatePicker ...................................................................................................... 187
5.13.1 PickerView(選擇器)簡介 ................................................................................... 187
5.13.2 使用delegate 和dataSource .................................................................................... 188
5.13.3 PickerView 的代碼實現 .......................................................................................... 191
5.13.4 DatePicker(日期選擇器)的屬性檢查器 ............................................................ 193
5.13.5 DatePicker 的代碼實現 ........................................................................................... 195
5.13.6 淺談“Delegate pattern”委託模式 ....................................................................... 195
5.14 AlertView&ActionSheet ...................................................................................................... 196
5.14.1 AlertView ................................................................................................................. 196
5.14.2 ActionSheet(操作表) .......................................................................................... 200
5.14.3 更好的選擇:UIAlertController ............................................................................. 201
5.15 多重MVC 及Segues(過渡) .......................................................................................... 205
5.15.1 多重MVC 與Segues 簡介 ..................................................................................... 205
5.15.2 NavigationController(導航控制器) .................................................................... 209
5.15.3 Segues(過渡) ...................................................................................................... 216
5.15.4 SplitViewController ................................................................................................. 224
5.15.5 TabBarController ........................................................................................................ 226
5.15.6 PageViewController ................................................................................................. 233
5.16 Toolbar ................................................................................................................................ 240
5.16.1 Toolbar 的屬性檢查器 ............................................................................................ 241
5.17 ScrollView ........................................................................................................................... 243
5.17.1 ScrollView 簡介 ...................................................................................................... 243
5.17.2 CGFloat、CGPoint、CGSize ................................................................................. 243
5.17.3 視圖的坐標系統...................................................................................................... 244
5.17.4 ScrollView 實戰 ...................................................................................................... 244
5.18 TableViewController ........................................................................................................... 247
5.18.1 UITabelView 簡介 ................................................................................................... 247
5.18.2 UITableViewCell 簡介 ............................................................................................ 250
5.18.3 TabelView 與cell 的互動 ....................................................................................... 251
Swift 開發手冊:技巧與實戰
VIII
5.19 SearchBar ............................................................................................................................. 256
5.19.1 SearchBar 的屬性檢查器 ........................................................................................ 256
5.19.2 實時顯示搜尋結果的SearchBar ............................................................................ 258
5.20 IOS 與Web .......................................................................................................................... 261
5.20.1 UIWebView ............................................................................................................. 261
5.20.2 WKWebView ........................................................................................................... 264
5.20.3 SFSafariViewController .......................................................................................... 265
5.21 AutoLayout&StackView ...................................................................................................... 267
5.21.1 AutoLayout .............................................................................................................. 267
5.21.2 UIStackView(堆疊視圖) .................................................................................... 277
第6章 IOS開發揭秘 ................................................................................................................ 281
6.1 Gestures——手勢 .................................................................................................................. 281
6.2 KVC 與API 設計 .................................................................................................................. 286
6.3 訪問簡訊與電話API ............................................................................................................ 290
6.3.1 使用URL 訪問簡訊與電話API .............................................................................. 290
6.3.2 MessageUI ................................................................................................................. 291
6.4 訪問相冊與相機API ............................................................................................................ 293
6.5 快速分享API .......................................................................................................................... 296
6.6 地圖與定位API ...................................................................................................................... 301
6.7 網路通信................................................................................................................................. 311
6.7.1 網路通信初探 ............................................................................................................. 312
6.7.2 NSURLConnection 還是NSURLSession ................................................................. 313
6.8 數據持久化 .............................................................................................................................. 317
6.8.1 Archiving(歸檔) ................................................................................................... 318
6.8.2 File System(檔案系統) ......................................................................................... 318
6.8.3 SQLite&Core Data ..................................................................................................... 321
6.9 多執行緒 .................................................................................................................................. 326
6.9.1 GCD ........................................................................................................................... 326
6.9.2 NSOperationQueue .................................................................................................... 329
6.10 Swifter 幫助貼士 ................................................................................................................. 331