diff --git a/chapter1/01_swift.html b/chapter1/01_swift.html index 6f69c9c9..47a1c05c 100644 --- a/chapter1/01_swift.html +++ b/chapter1/01_swift.html @@ -34,7 +34,7 @@ -
翻译:numbbbbb
diff --git a/chapter1/02_a_swift_tour.html b/chapter1/02_a_swift_tour.html index 9aa932b5..8ec50b79 100644 --- a/chapter1/02_a_swift_tour.html +++ b/chapter1/02_a_swift_tour.html @@ -34,7 +34,7 @@ -
校对:yeahdongcn+@@ -595,10 +595,11 @@-+ - +Swift 初见
@@ -618,8 +619,9 @@如果你写过 C 或者 Objective-C 代码,那你应该很熟悉这种形式——在 Swift 中,这行代码就是一个完整的程序。你不需要为了输入输出或者字符串处理导入一个单独的库。全局作用域中的代码会被自动当做程序的入口点,所以你也不需要
main函数。你同样不需要在每个语句结尾写上分号。这个教程会通过一系列编程例子来让你对 Swift 有初步了解,如果你有什么不理解的地方也不用担心——任何本章介绍的内容都会在后面的章节中详细讲解。
-注意:
+
为了获得最好的体验,在 Xcode 当中使用代码预览功能。代码预览功能可以让你编辑代码并实时看到运行结果。 -打开Playground注意: +为了获得最好的体验,在 Xcode 当中使用代码预览功能。代码预览功能可以让你编辑代码并实时看到运行结果。 +打开Playground
简单值
@@ -635,7 +637,8 @@ let implicitDouble = 70.0 let explicitDouble: Double = 70-练习:
+
创建一个常量,显式指定类型为Float并指定初始值为4。练习: +创建一个常量,显式指定类型为
Float并指定初始值为4。值永远不会被隐式转换为其他类型。如果你需要把一个值转换成其他类型,请显式转换。
let label = "The width is" @@ -643,7 +646,8 @@ let width = 94 let widthLabel = label + String(width)-练习:
+
删除最后一行中的String,错误提示是什么?练习: +删除最后一行中的
String,错误提示是什么?有一种更简单的把值转换成字符串的方法:把值写到括号中,并且在括号之前写一个反斜杠。例如:
let apples = 3 @@ -652,7 +656,8 @@ let appleSummary = "I have \(apples) apples." let fruitSummary = "I have \(apples + oranges) pieces of fruit."-练习:
+
使用\()来把一个浮点计算转换成字符串,并加上某人的名字,和他打个招呼。练习: +使用
\()来把一个浮点计算转换成字符串,并加上某人的名字,和他打个招呼。使用方括号
[]来创建数组和字典,并使用下标或者键(key)来访问元素。var shoppingList = ["catfish", "water", "tulips", "blue paint"] @@ -697,7 +702,8 @@ if let name = optionalName { }-练习:
+
把optionalName改成nil,greeting会是什么?添加一个else语句,当optionalName是nil时给greeting赋一个不同的值。练习: +把
optionalName改成nil,greeting会是什么?添加一个else语句,当optionalName是nil时给greeting赋一个不同的值。如果变量的可选值是
nil,条件会判断为false,大括号中的代码会被跳过。如果不是nil,会将值赋给let后面的常量,这样代码块中就可以使用这个值了。@@ -714,7 +720,8 @@ default: }
switch支持任意类型的数据以及各种比较操作——不仅仅是整数以及测试相等。-练习:
+
删除default语句,看看会有什么错误?练习: +删除
default语句,看看会有什么错误?运行
switch中匹配到的子句之后,程序会退出switch语句,并不会继续向下运行,所以不需要在每个子句结尾写break。你可以使用
@@ -734,7 +741,8 @@ for (kind, numbers) in interestingNumbers { largestfor-in来遍历字典,需要两个变量来表示每个键值对。-练习:
+
添加另一个变量来记录哪种类型的数字是最大的。练习: +添加另一个变量来记录哪种类型的数字是最大的。
使用
while来重复运行一段代码直到不满足条件。循环条件可以在开头也可以在结尾。var n = 2 @@ -772,7 +780,8 @@ secondForLoop greet("Bob", "Tuesday")-练习:
+
删除day参数,添加一个参数来表示今天吃了什么午饭。练习: +删除
day参数,添加一个参数来表示今天吃了什么午饭。使用一个元组来返回多个值。
func getGasPrices() -> (Double, Double, Double) { @@ -792,7 +801,8 @@ sumOf() sumOf(42, 597, 12)-练习:
+
写一个计算参数平均值的函数。练习: +写一个计算参数平均值的函数。
函数可以嵌套。被嵌套的函数可以访问外侧函数的变量,你可以使用嵌套函数来重构一个太长或者太复杂的函数。
func returnFifteen() -> Int { @@ -838,7 +848,8 @@ hasAnyMatches(numbers, lessThanTen) })-练习:
+
重写闭包,对所有奇数返回0。练习: +重写闭包,对所有奇数返回0。
有很多种创建闭包的方法。如果一个闭包的类型已知,比如作为一个回调函数,你可以忽略参数的类型和返回值。单个语句闭包会把它语句的值当做结果返回。
numbers.map({ number in 3 * number }) @@ -857,7 +868,8 @@ hasAnyMatches(numbers, lessThanTen) }-练习:
+
使用let添加一个常量属性,再添加一个接收一个参数的方法。练习: +使用
let添加一个常量属性,再添加一个接收一个参数的方法。要创建一个类的实例,在类名后面加上括号。使用点语法来访问实例的属性和方法。
var shape = Shape() @@ -904,7 +916,8 @@ test.area() test.simpleDescription()-练习:
+
创建NamedShape的另一个子类Circle,构造器接收两个参数,一个是半径一个是名称,实现area和describe方法。练习: +创建
NamedShape的另一个子类Circle,构造器接收两个参数,一个是半径一个是名称,实现area和describe方法。属性可以有 getter 和 setter 。
class EquilateralTriangle: NamedShape { @@ -1005,7 +1018,8 @@ let ace = Rank.Ace let aceRawValue = ace.toRaw()-练习:
+
写一个函数,通过比较它们的原始值来比较两个Rank值。练习: +写一个函数,通过比较它们的原始值来比较两个
Rank值。在上面的例子中,枚举原始值的类型是
Int,所以你只需要设置第一个原始值。剩下的原始值会按照顺序赋值。你也可以使用字符串或者浮点数作为枚举的原始值。使用
@@ -1034,7 +1048,8 @@ let hearts = Suit.Hearts let heartsDescription = hearts.simpleDescription()toRaw和fromRaw函数来在原始值和枚举值之间进行转换。-练习:
+
给Suit添加一个color方法,对spades和clubs返回“black”,对hearts和diamonds返回“red”。练习: +给
Suit添加一个color方法,对spades和clubs返回“black”,对hearts和diamonds返回“red”。注意,有两种方式可以引用
Hearts成员:给hearts常量赋值时,枚举成员Suit.Hearts需要用全名来引用,因为常量没有显式指定类型。在switch里,枚举成员使用缩写.Hearts来引用,因为self的值已经知道是一个suit。已知变量类型的情况下你可以使用缩写。使用
struct来创建一个结构体。结构体和类有很多相同的地方,比如方法和构造器。它们之间最大的一个区别就是 @@ -1051,7 +1066,8 @@ let threeOfSpades = Card(rank: .Three, suit: .Spades) let threeOfSpadesDescription = threeOfSpades.simpleDescription()-练习:
+
给Card添加一个方法,创建一副完整的扑克牌并把每张牌的 rank 和 suit 对应起来。练习: +给
Card添加一个方法,创建一副完整的扑克牌并把每张牌的 rank 和 suit 对应起来。一个枚举成员的实例可以有实例值。相同枚举成员的实例可以有不同的值。创建实例的时候传入值即可。实例值和原始值是不同的:枚举成员的原始值对于所有实例都是相同的,而且你是在定义枚举的时候设置原始值。
例如,考虑从服务器获取日出和日落的时间。服务器会返回正常结果或者错误信息。
@@ -1071,7 +1087,8 @@ case let .Error(error): }-练习:
+
给ServerResponse和switch添加第三种情况。练习: +给
ServerResponse和switch添加第三种情况。注意如何从
@@ -1105,7 +1122,8 @@ b.adjust() let bDescription = b.simpleDescriptionServerResponse中提取日升和日落时间。-练习:
+
写一个实现这个协议的枚举。练习: +写一个实现这个协议的枚举。
注意声明
SimpleStructure时候mutating关键字用来标记一个会修改结构体的方法。SimpleClass的声明不需要标记任何方法因为类中的方法经常会修改类。使用
@@ -1120,7 +1138,8 @@ let bDescription = b.simpleDescription 7.simpleDescriptionextension来为现有的类型添加功能,比如新的方法和参数。你可以使用扩展来改造定义在别处,甚至是从外部库或者框架引入的一个类型,使得这个类型遵循某个协议。-练习:
+
给Double类型写一个扩展,添加absoluteValue功能。练习: +给
Double类型写一个扩展,添加absoluteValue功能。你可以像使用其他命名类型一样使用协议名——例如,创建一个有不同类型但是都实现一个协议的对象集合。当你处理类型是协议的值时,协议外定义的方法不可用。
let protocolValue: ExampleProtocol = a @@ -1163,7 +1182,8 @@ possibleInteger = .Some(100) anyCommonElements([1, 2, 3], [3])-练习:
+
修改anyCommonElements函数来创建一个函数,返回一个数组,内容是两个序列的共有元素。练习: +修改
anyCommonElements函数来创建一个函数,返回一个数组,内容是两个序列的共有元素。简单起见,你可以忽略
diff --git a/chapter1/chapter1.html b/chapter1/chapter1.html index b31a3129..cb69e1ef 100644 --- a/chapter1/chapter1.html +++ b/chapter1/chapter1.html @@ -34,7 +34,7 @@ -where,只在冒号后面写协议或者类名。<T: Equatable>和<T where T: Equatable>是等价的。+@@ -595,7 +595,7 @@-+ 欢迎使用 Swift
在本章中您将了解 Swift 的特性和开发历史,并对 Swift 有一个初步的了解。
diff --git a/chapter2/01_The_Basics.html b/chapter2/01_The_Basics.html index 219b7dc6..b3d35a9c 100644 --- a/chapter2/01_The_Basics.html +++ b/chapter2/01_The_Basics.html @@ -34,7 +34,7 @@ -+@@ -595,7 +595,7 @@-+ 翻译:numbbbbb, lyuka, JaySurplus
diff --git a/chapter2/02_Basic_Operators.html b/chapter2/02_Basic_Operators.html index c1c9028f..69e7a618 100644 --- a/chapter2/02_Basic_Operators.html +++ b/chapter2/02_Basic_Operators.html @@ -34,7 +34,7 @@ -
校对:lslxdx+@@ -595,7 +595,7 @@-+ 翻译:xielingwang
diff --git a/chapter2/03_Strings_and_Characters.html b/chapter2/03_Strings_and_Characters.html index 30533193..e992b2b5 100644 --- a/chapter2/03_Strings_and_Characters.html +++ b/chapter2/03_Strings_and_Characters.html @@ -34,7 +34,7 @@ -
校对:Evilcome+@@ -595,7 +595,7 @@-+ diff --git a/chapter2/04_Collection_Types.html b/chapter2/04_Collection_Types.html index 9af10b23..d9ea3e55 100644 --- a/chapter2/04_Collection_Types.html +++ b/chapter2/04_Collection_Types.html @@ -34,7 +34,7 @@ -+@@ -595,7 +595,7 @@-+ diff --git a/chapter2/05_Control_Flow.html b/chapter2/05_Control_Flow.html index 87dddc39..9c97a817 100644 --- a/chapter2/05_Control_Flow.html +++ b/chapter2/05_Control_Flow.html @@ -34,7 +34,7 @@ -+@@ -595,7 +595,7 @@-+ 翻译:vclwei, coverxit, NicePiao
diff --git a/chapter2/06_Functions.html b/chapter2/06_Functions.html index 0c692f73..a00940c0 100644 --- a/chapter2/06_Functions.html +++ b/chapter2/06_Functions.html @@ -34,7 +34,7 @@ -
校对:coverxit, stanzhai+@@ -595,7 +595,7 @@-+ diff --git a/chapter2/07_Closures.html b/chapter2/07_Closures.html index 29fa2531..4e5dfa80 100644 --- a/chapter2/07_Closures.html +++ b/chapter2/07_Closures.html @@ -34,7 +34,7 @@ -+@@ -595,7 +595,7 @@-+ diff --git a/chapter2/08_Enumerations.html b/chapter2/08_Enumerations.html index 178211b3..9cb71525 100644 --- a/chapter2/08_Enumerations.html +++ b/chapter2/08_Enumerations.html @@ -34,7 +34,7 @@ -+@@ -595,7 +595,7 @@-+ 翻译:yankuangshi
diff --git a/chapter2/09_Classes_and_Structures.html b/chapter2/09_Classes_and_Structures.html index 3ef6006b..378dcea8 100644 --- a/chapter2/09_Classes_and_Structures.html +++ b/chapter2/09_Classes_and_Structures.html @@ -34,7 +34,7 @@ -
校对:shinyzhu+@@ -595,7 +595,7 @@-+ 翻译:JaySurplus
diff --git a/chapter2/10_Properties.html b/chapter2/10_Properties.html index 98f8ce92..0431b1ee 100644 --- a/chapter2/10_Properties.html +++ b/chapter2/10_Properties.html @@ -34,7 +34,7 @@ -
校对:sg552+@@ -595,7 +595,7 @@-+ diff --git a/chapter2/11_Methods.html b/chapter2/11_Methods.html index 1bba8979..82eb5504 100644 --- a/chapter2/11_Methods.html +++ b/chapter2/11_Methods.html @@ -34,7 +34,7 @@ -+@@ -595,7 +595,7 @@-+ diff --git a/chapter2/12_Subscripts.html b/chapter2/12_Subscripts.html index 7aac7395..e539b386 100644 --- a/chapter2/12_Subscripts.html +++ b/chapter2/12_Subscripts.html @@ -34,7 +34,7 @@ -+@@ -595,7 +595,7 @@-+ diff --git a/chapter2/13_Inheritance.html b/chapter2/13_Inheritance.html index a50e0f70..e74e2ec8 100644 --- a/chapter2/13_Inheritance.html +++ b/chapter2/13_Inheritance.html @@ -34,7 +34,7 @@ -+@@ -595,7 +595,7 @@-+ 翻译:Hawstein
diff --git a/chapter2/14_Initialization.html b/chapter2/14_Initialization.html index 559800b2..47707e6a 100644 --- a/chapter2/14_Initialization.html +++ b/chapter2/14_Initialization.html @@ -34,7 +34,7 @@ -
校对:menlongsheng+@@ -595,7 +595,7 @@-+ diff --git a/chapter2/15_Deinitialization.html b/chapter2/15_Deinitialization.html index ab59d67e..ffb365e5 100644 --- a/chapter2/15_Deinitialization.html +++ b/chapter2/15_Deinitialization.html @@ -34,7 +34,7 @@ -+@@ -595,7 +595,7 @@-+ diff --git a/chapter2/16_Automatic_Reference_Counting.html b/chapter2/16_Automatic_Reference_Counting.html index 2fb7494c..9e8cc76b 100644 --- a/chapter2/16_Automatic_Reference_Counting.html +++ b/chapter2/16_Automatic_Reference_Counting.html @@ -34,7 +34,7 @@ -+@@ -595,7 +595,7 @@-+ diff --git a/chapter2/17_Optional_Chaining.html b/chapter2/17_Optional_Chaining.html index 6d38c616..42d203f6 100644 --- a/chapter2/17_Optional_Chaining.html +++ b/chapter2/17_Optional_Chaining.html @@ -34,7 +34,7 @@ -+@@ -595,7 +595,7 @@-+ 翻译:Jasonbroker
diff --git a/chapter2/18_Type_Casting.html b/chapter2/18_Type_Casting.html index b081cb96..63f43831 100644 --- a/chapter2/18_Type_Casting.html +++ b/chapter2/18_Type_Casting.html @@ -34,7 +34,7 @@ -
校对:numbbbbb, stanzhai+@@ -595,7 +595,7 @@-+ 翻译:xiehurricane
diff --git a/chapter2/19_Nested_Types.html b/chapter2/19_Nested_Types.html index 857a13f5..43dabf4f 100644 --- a/chapter2/19_Nested_Types.html +++ b/chapter2/19_Nested_Types.html @@ -34,7 +34,7 @@ -
校对:happyming+@@ -595,7 +595,7 @@-+ diff --git a/chapter2/20_Extensions.html b/chapter2/20_Extensions.html index 5eb4c318..618294e7 100644 --- a/chapter2/20_Extensions.html +++ b/chapter2/20_Extensions.html @@ -34,7 +34,7 @@ -+@@ -595,7 +595,7 @@-+ diff --git a/chapter2/21_Protocols.html b/chapter2/21_Protocols.html index f38ed984..dbb7fe85 100644 --- a/chapter2/21_Protocols.html +++ b/chapter2/21_Protocols.html @@ -34,7 +34,7 @@ -+@@ -595,7 +595,7 @@-+ 翻译:geek5nan
diff --git a/chapter2/22_Generics.html b/chapter2/22_Generics.html index 81564d7d..4c7255d1 100644 --- a/chapter2/22_Generics.html +++ b/chapter2/22_Generics.html @@ -34,7 +34,7 @@ -
校对:dabing1022+@@ -595,7 +595,7 @@-+ diff --git a/chapter2/23_Advanced_Operators.html b/chapter2/23_Advanced_Operators.html index 7b139bdb..c458d68c 100644 --- a/chapter2/23_Advanced_Operators.html +++ b/chapter2/23_Advanced_Operators.html @@ -34,7 +34,7 @@ -+@@ -595,7 +595,7 @@-+ 翻译:xielingwang
diff --git a/chapter2/chapter2.html b/chapter2/chapter2.html index 47d49591..26cfb019 100644 --- a/chapter2/chapter2.html +++ b/chapter2/chapter2.html @@ -34,7 +34,7 @@ -
校对:numbbbbb+@@ -595,7 +595,7 @@-+ Swift 教程
本章介绍了 Swift 的各种特性及其使用方法,是全书的核心部分。
diff --git a/chapter3/01_About_the_Language_Reference.html b/chapter3/01_About_the_Language_Reference.html index 06cf6b3a..821f4a44 100644 --- a/chapter3/01_About_the_Language_Reference.html +++ b/chapter3/01_About_the_Language_Reference.html @@ -34,7 +34,7 @@ -+@@ -595,7 +595,7 @@-+ 翻译:dabing1022
diff --git a/chapter3/02_Lexical_Structure.html b/chapter3/02_Lexical_Structure.html index 9b07e3a0..c51f771b 100644 --- a/chapter3/02_Lexical_Structure.html +++ b/chapter3/02_Lexical_Structure.html @@ -34,7 +34,7 @@ -
校对:numbbbbb+@@ -595,7 +595,7 @@-+ diff --git a/chapter3/03_Types.html b/chapter3/03_Types.html index e3f9150d..de06817a 100644 --- a/chapter3/03_Types.html +++ b/chapter3/03_Types.html @@ -34,7 +34,7 @@ -+@@ -595,7 +595,7 @@-+ diff --git a/chapter3/04_Expressions.html b/chapter3/04_Expressions.html index f2f9cf05..ed2b27d6 100644 --- a/chapter3/04_Expressions.html +++ b/chapter3/04_Expressions.html @@ -34,7 +34,7 @@ -+@@ -595,7 +595,7 @@-+ diff --git a/chapter3/05_Declarations.html b/chapter3/05_Declarations.html index e8399686..db33869d 100644 --- a/chapter3/05_Declarations.html +++ b/chapter3/05_Declarations.html @@ -34,7 +34,7 @@ -+@@ -595,7 +595,7 @@-+ 翻译:marsprince
diff --git a/chapter3/06_Attributes.html b/chapter3/06_Attributes.html index 453e7f24..70529fb6 100644 --- a/chapter3/06_Attributes.html +++ b/chapter3/06_Attributes.html @@ -34,7 +34,7 @@ -
校对:numbbbbb, stanzhai+@@ -595,7 +595,7 @@-+ 翻译:Hawstein
diff --git a/chapter3/07_Patterns.html b/chapter3/07_Patterns.html index af78f04d..1868ea9e 100644 --- a/chapter3/07_Patterns.html +++ b/chapter3/07_Patterns.html @@ -34,7 +34,7 @@ -
校对:numbbbbb, stanzhai+@@ -595,7 +595,7 @@-+ 翻译:honghaoz
diff --git a/chapter3/08_Generic_Parameters_and_Arguments.html b/chapter3/08_Generic_Parameters_and_Arguments.html index a651635c..a14017b8 100644 --- a/chapter3/08_Generic_Parameters_and_Arguments.html +++ b/chapter3/08_Generic_Parameters_and_Arguments.html @@ -34,7 +34,7 @@ -
校对:numbbbbb, stanzhai+@@ -595,7 +595,7 @@-+ 翻译:fd5788
diff --git a/chapter3/09_Summary_of_the_Grammar.html b/chapter3/09_Summary_of_the_Grammar.html index 4ef6bdf4..108e932a 100644 --- a/chapter3/09_Summary_of_the_Grammar.html +++ b/chapter3/09_Summary_of_the_Grammar.html @@ -32,7 +32,7 @@ -
校对:yankuangshi, stanzhai+@@ -593,7 +593,7 @@-+ 翻译:stanzhai
diff --git a/chapter3/10_Statements.html b/chapter3/10_Statements.html index 614d17ca..88c9fa67 100644 --- a/chapter3/10_Statements.html +++ b/chapter3/10_Statements.html @@ -34,7 +34,7 @@ -
校对:xielingwang