logo资料库

Swift中文手册(完整版).pdf

第1页 / 共411页
第2页 / 共411页
第3页 / 共411页
第4页 / 共411页
第5页 / 共411页
第6页 / 共411页
第7页 / 共411页
第8页 / 共411页
资料共411页,剩余部分请下载后查看
触控科技 The Swift Programming Language 中文版 (CocoaChina 精校) CocoaChina 2014-6-12
本 教 程 系 列 由 CocoaChina 整 理 自 CocoaChina 的 github 和 numbbbbb 的 github ,禁止商用,特此声明。 微博:@CocoaChina 微信:CocoaChina 整理人:齐楠楠 The Swift Programming Language 中文 CocoaChina 精校版 1
目录 1. 欢迎使⽤Swift .............................................................................................................. 8 1.1.关于 Swift ............................................................................................................ 8 1.2. Swift 初见............................................................................................................ 9 简单值 ............................................................................................................... 9 控制流 ............................................................................................................. 11 函数和闭包 ...................................................................................................... 14 对象和类.......................................................................................................... 16 枚举和结构体................................................................................................... 20 接口和扩展 ...................................................................................................... 23 泛型................................................................................................................. 25 2. Swift 教程 .................................................................................................................. 26 2.1 基础部分 ........................................................................................................... 26 常量和变量 ...................................................................................................... 27 注释................................................................................................................. 30 分号................................................................................................................. 31 整数................................................................................................................. 31 浮点数 ............................................................................................................. 32 类型安全和类型推测 ........................................................................................ 32 数值型字面量................................................................................................... 33 数值型类型转换 ............................................................................................... 35 类型别名.......................................................................................................... 36 布尔值 ............................................................................................................. 37 元组................................................................................................................. 38 可选................................................................................................................. 40 断言................................................................................................................. 44 2.2. 基本运算符 ...................................................................................................... 45 术语................................................................................................................. 46 赋值运算符 ...................................................................................................... 47 数值运算.......................................................................................................... 47 复合赋值.......................................................................................................... 51 比较运算.......................................................................................................... 51 三目条件运算................................................................................................... 52 区间运算符 ...................................................................................................... 53 逻辑运算.......................................................................................................... 54 2.3. 字符串和字符................................................................................................... 57 字符串字面量................................................................................................... 58 The Swift Programming Language 中文 CocoaChina 精校版 2
初始化空字符串 ............................................................................................... 59 字符串可变性................................................................................................... 59 字符串是值类型 ............................................................................................... 60 使用字符(Characters) ...................................................................................... 60 计算字符数量................................................................................................... 61 连接字符串和字符............................................................................................ 61 字符串插值 ...................................................................................................... 62 比较字符串 ...................................................................................................... 63 大写和小写字符串............................................................................................ 65 Unicode ........................................................................................................... 65 2.4. 集合类型.......................................................................................................... 68 数组................................................................................................................. 68 字典................................................................................................................. 74 集合的可变性................................................................................................... 79 2.5. 控制流 ............................................................................................................. 79 For 循环.......................................................................................................... 80 While 循环 ...................................................................................................... 84 条件语句.......................................................................................................... 88 控制转移语句................................................................................................... 97 2.6. 函数................................................................................................................103 定义和调用函数 ..............................................................................................104 函数的形参和返回值 .......................................................................................105 函数形参名 .....................................................................................................108 函数类型.........................................................................................................115 嵌套函数.........................................................................................................119 2.7. 闭包................................................................................................................119 闭包表达式 .....................................................................................................120 Trailing 闭包...................................................................................................124 捕获 (Caputure) .............................................................................................127 闭包是引用类型 ..............................................................................................129 2.8. 枚举................................................................................................................130 枚举语法(Enumeration Syntax) ..................................................................130 匹配枚举值和 Switch 语句...............................................................................132 关联值(Associated Values).........................................................................133 原始值(Raw Values)...................................................................................135 2.9. 类和结构体 .....................................................................................................137 类和结构体对比 ..............................................................................................138 结构体和枚举是值类型....................................................................................141 类是引用类型..................................................................................................143 类和结构体的选择...........................................................................................145 The Swift Programming Language 中文 CocoaChina 精校版 3
集合(Collection)类型的赋值和拷贝行为 ...........................................................146 2.10. 属性 ..............................................................................................................151 存储属性.........................................................................................................152 计算属性.........................................................................................................155 属性监视器 .....................................................................................................158 全局变量和局部变量 .......................................................................................160 类型属性.........................................................................................................160 2.11. ⽅法 ..............................................................................................................165 实例方法(Instance Methods)...........................................................................165 类型方法(Type Methods) ................................................................................171 2.12. 附属脚本 .......................................................................................................174 附属脚本语法..................................................................................................174 附属脚本用法..................................................................................................176 附属脚本选项..................................................................................................176 2.13. 继承 ..............................................................................................................178 定义一个基类(Base class)..........................................................................179 子类生成(Subclassing)...............................................................................180 重写(Overriding) ........................................................................................182 防止重写.........................................................................................................186 2.14. 构造过程 .......................................................................................................187 存储型属性的初始赋值....................................................................................187 定制化构造过程 ..............................................................................................189 默认构造器 .....................................................................................................192 值类型的构造器代理 .......................................................................................193 类的继承和构造过程 .......................................................................................196 通过闭包和函数来设置属性的默认值...............................................................207 2.15. 反初始化 .......................................................................................................209 反初始化.........................................................................................................209 反初始化原理..................................................................................................209 2.16. 自动引用计数 ................................................................................................212 ARC 如何工作 ................................................................................................213 ARC 实践 .......................................................................................................214 类实例间的强引用环 .......................................................................................215 解决实例间的强引用环....................................................................................218 解决闭包产生的强引用环 ................................................................................228 2.17. 自判断链接 ...................................................................................................230 自判断链接可替代强制拆包.............................................................................231 为自判断链接定义模型类 ................................................................................233 通过自判断链接调用属性 ................................................................................235 The Swift Programming Language 中文 CocoaChina 精校版 4
通过自判断链接调用方法 ................................................................................235 使用自判断链接调用子脚本.............................................................................236 连接多层链接..................................................................................................237 链接自判断返回值的方法 ................................................................................238 2.18. 类型转换 .......................................................................................................239 定义一个类层次作为例子 ................................................................................239 检查类型.........................................................................................................241 向下转型(简称下转)....................................................................................242 Any 和 AnyObject 的转换 ................................................................................243 2.19 类型嵌套........................................................................................................246 类型嵌套实例..................................................................................................246 类型嵌套的引用 ..............................................................................................248 2.20 扩展...............................................................................................................249 扩展语法(Extension Syntax) ..........................................................................249 计算型属性(Computed Properties) .............................................................250 构造器(Initializers)......................................................................................251 方法(Methods) ...........................................................................................252 下标(Subscripts) ........................................................................................254 嵌套类型(Nested Types) ............................................................................255 2.21 协议...............................................................................................................256 协议的语法 .....................................................................................................256 属性要求.........................................................................................................257 方法要求.........................................................................................................258 突变方法要求..................................................................................................259 协议类型.........................................................................................................261 委托(代理)模式 ...............................................................................................262 在扩展中添加协议成员....................................................................................265 通过延展补充协议声明....................................................................................266 集合中的协议类型...........................................................................................267 协议的继承 .....................................................................................................267 协议合成.........................................................................................................269 检验协议的一致性...........................................................................................270 可选协议要求..................................................................................................271 2.22 泛型...............................................................................................................274 泛型所解决的问题...........................................................................................274 泛型函数.........................................................................................................276 类型参数.........................................................................................................277 命名类型参数..................................................................................................277 泛型类型.........................................................................................................278 类型约束.........................................................................................................281 The Swift Programming Language 中文 CocoaChina 精校版 5
关联类型.........................................................................................................284 Where 语句 ....................................................................................................287 2.23 高级运算符 ....................................................................................................290 位运算符.........................................................................................................291 溢出运算符 .....................................................................................................296 优先级和结合性 ..............................................................................................299 运算符函数 .....................................................................................................300 自定义运算符..................................................................................................304 3 语言附注 .............................................................................................................305 3.1 关于语言附注 ...................................................................................................305 如何阅读语法..................................................................................................306 3.2 词法结构 .........................................................................................................307 空白与注释 .....................................................................................................307 标识符 ............................................................................................................307 关键字 ............................................................................................................309 字面量 ............................................................................................................309 运算符 ............................................................................................................313 3.3 类型 ................................................................................................................314 类型注解.........................................................................................................315 类型标识符 .....................................................................................................316 元组类型.........................................................................................................316 函数类型.........................................................................................................317 数组类型.........................................................................................................319 可选类型.........................................................................................................319 隐式解析可选类型...........................................................................................320 协议合成类型..................................................................................................321 元类型 ............................................................................................................322 类型继承子句..................................................................................................323 类型推断.........................................................................................................323 3.4 表达式 .............................................................................................................324 前缀表达式(Prefix Expressions).....................................................................325 二元表达式(Binary Expressions)....................................................................325 赋值表达式(Assignment Operator) ................................................................328 三元条件运算符(Ternary Conditional Operator)..............................................328 类型转换运算符(Type-Casting Operators) ......................................................329 主要表达式(Primary Expressions)..................................................................330 后缀表达式(Postfix Expressions) ...................................................................336 3.5 语句 ................................................................................................................342 循环语句.........................................................................................................343 分支语句.........................................................................................................346 The Swift Programming Language 中文 CocoaChina 精校版 6
带标签的语句..................................................................................................350 3.6 声明 ................................................................................................................354 模块范围.........................................................................................................355 代码块 ............................................................................................................356 引入声明.........................................................................................................356 常量声明.........................................................................................................357 变量声明.........................................................................................................358 类型的别名声明 ..............................................................................................362 函数声明.........................................................................................................363 枚举声明.........................................................................................................367 结构体声明 .....................................................................................................369 协议声明.........................................................................................................372 构造器声明 .....................................................................................................376 析构声明.........................................................................................................377 扩展声明.........................................................................................................378 附属脚本声明..................................................................................................378 运算符声明 .....................................................................................................379 3.7 特性 ................................................................................................................381 声明特性.........................................................................................................382 类型特性.........................................................................................................385 3.8 模式 ................................................................................................................385 通配符模式(Wildcard Pattern) ......................................................................386 标识符模式(Identifier Pattern) ......................................................................387 值绑定模式(Value-Binding Pattern) ...............................................................387 元组模式(Tuple Pattern)...............................................................................388 枚举案例模式(Enumeration Case Pattern)......................................................389 类型转换模式(Type-Casting Patterns) ............................................................389 表达式模式(Expression Pattern)....................................................................390 3.9 泛型参数 .........................................................................................................391 泛型形参语句..................................................................................................391 泛型实参语句..................................................................................................393 3.10 语法总结........................................................................................................394 语句................................................................................................................395 泛型参数.........................................................................................................396 声明................................................................................................................397 模式................................................................................................................402 特性................................................................................................................403 表达式 ............................................................................................................403 词法结构.........................................................................................................406 类型................................................................................................................409 The Swift Programming Language 中文 CocoaChina 精校版 7
分享到:
收藏