修正一些编辑中的小错误 (#883)
* 04_Collection_Types 优化翻译细节 * 修正注释引号使用 * 05_Control_Flow 更新代码 * 修正注释中引号的使用
This commit is contained in:
@ -119,7 +119,7 @@ languageName = "Swift++"
|
||||
|
||||
```swift
|
||||
print(friendlyWelcome)
|
||||
// 输出 "Bonjour!"
|
||||
// 输出“Bonjour!”
|
||||
```
|
||||
|
||||
`print(_:separator:terminator:)` 是一个用来输出一个或多个值到适当输出区的全局函数。如果你用 Xcode,`print(_:separator:terminator:)` 将会输出内容到“console”面板上。`separator` 和 `terminator` 参数具有默认值,因此你调用这个函数的时候可以忽略它们。默认情况下,该函数通过添加换行符来结束当前行。如果不想换行,可以传递一个空字符串给 `terminator` 参数--例如,`print(someValue, terminator:"")` 。关于参数默认值的更多信息,请参考[默认参数值](./06_Functions.html#default_parameter_values)。
|
||||
@ -128,7 +128,7 @@ Swift 用*字符串插值(string interpolation)*的方式把常量名或者
|
||||
|
||||
```swift
|
||||
print("The current value of friendlyWelcome is \(friendlyWelcome)")
|
||||
// 输出 "The current value of friendlyWelcome is Bonjour!
|
||||
// 输出“The current value of friendlyWelcome is Bonjour!”
|
||||
```
|
||||
|
||||
> 注意
|
||||
@ -170,7 +170,7 @@ Swift 中的注释与 C 语言的注释非常相似。单行注释以双正斜
|
||||
|
||||
```swift
|
||||
let cat = "🐱"; print(cat)
|
||||
// 输出 "🐱"
|
||||
// 输出“🐱”
|
||||
```
|
||||
|
||||
<a name="integers"></a>
|
||||
@ -413,7 +413,7 @@ if turnipsAreDelicious {
|
||||
} else {
|
||||
print("Eww, turnips are horrible.")
|
||||
}
|
||||
// 输出 "Eww, turnips are horrible."
|
||||
// 输出“Eww, turnips are horrible.”
|
||||
```
|
||||
|
||||
条件语句,例如 `if`,请参考[控制流](./05_Control_Flow.html)。
|
||||
@ -461,9 +461,9 @@ let http404Error = (404, "Not Found")
|
||||
```swift
|
||||
let (statusCode, statusMessage) = http404Error
|
||||
print("The status code is \(statusCode)")
|
||||
// 输出 "The status code is 404"
|
||||
// 输出“The status code is 404”
|
||||
print("The status message is \(statusMessage)")
|
||||
// 输出 "The status message is Not Found"
|
||||
// 输出“The status message is Not Found”
|
||||
```
|
||||
|
||||
如果你只需要一部分元组值,分解的时候可以把要忽略的部分用下划线(`_`)标记:
|
||||
@ -471,16 +471,16 @@ print("The status message is \(statusMessage)")
|
||||
```swift
|
||||
let (justTheStatusCode, _) = http404Error
|
||||
print("The status code is \(justTheStatusCode)")
|
||||
// 输出 "The status code is 404"
|
||||
// 输出“The status code is 404”
|
||||
```
|
||||
|
||||
此外,你还可以通过下标来访问元组中的单个元素,下标从零开始:
|
||||
|
||||
```swift
|
||||
print("The status code is \(http404Error.0)")
|
||||
// 输出 "The status code is 404"
|
||||
// 输出“The status code is 404”
|
||||
print("The status message is \(http404Error.1)")
|
||||
// 输出 "The status message is Not Found"
|
||||
// 输出“The status message is Not Found”
|
||||
```
|
||||
|
||||
你可以在定义元组的时候给单个元素命名:
|
||||
@ -493,9 +493,9 @@ let http200Status = (statusCode: 200, description: "OK")
|
||||
|
||||
```swift
|
||||
print("The status code is \(http200Status.statusCode)")
|
||||
// 输出 "The status code is 200"
|
||||
// 输出“The status code is 200”
|
||||
print("The status message is \(http200Status.description)")
|
||||
// 输出 "The status message is OK"
|
||||
// 输出“The status message is OK”
|
||||
```
|
||||
|
||||
作为函数返回值时,元组非常有用。一个用来获取网页的函数可能会返回一个 `(Int, String)` 元组来描述是否获取成功。和只能返回一个类型的值比较起来,一个包含两个不同类型值的元组可以让函数的返回信息更有用。请参考[函数参数与返回值](./06_Functions.html#Function_Parameters_and_Return_Values)。
|
||||
@ -564,7 +564,7 @@ var surveyAnswer: String?
|
||||
if convertedNumber != nil {
|
||||
print("convertedNumber contains some integer value.")
|
||||
}
|
||||
// 输出 "convertedNumber contains some integer value."
|
||||
// 输出“convertedNumber contains some integer value.”
|
||||
```
|
||||
|
||||
当你确定可选类型确实包含值之后,你可以在可选的名字后面加一个感叹号(`!`)来获取值。这个惊叹号表示“我知道这个可选有值,请使用它。”这被称为可选值的*强制解析(forced unwrapping)*:
|
||||
@ -573,7 +573,7 @@ if convertedNumber != nil {
|
||||
if convertedNumber != nil {
|
||||
print("convertedNumber has an integer value of \(convertedNumber!).")
|
||||
}
|
||||
// 输出 "convertedNumber has an integer value of 123."
|
||||
// 输出“convertedNumber has an integer value of 123.”
|
||||
```
|
||||
|
||||
更多关于 `if` 语句的内容,请参考[控制流](./05_Control_Flow.html)。
|
||||
@ -603,7 +603,7 @@ if let actualNumber = Int(possibleNumber) {
|
||||
} else {
|
||||
print("\'\(possibleNumber)\' could not be converted to an integer")
|
||||
}
|
||||
// 输出 "'123' has an integer value of 123"
|
||||
// 输出“'123' has an integer value of 123”
|
||||
```
|
||||
|
||||
这段代码可以被理解为:
|
||||
@ -620,7 +620,7 @@ if let actualNumber = Int(possibleNumber) {
|
||||
if let firstNumber = Int("4"), let secondNumber = Int("42"), firstNumber < secondNumber && secondNumber < 100 {
|
||||
print("\(firstNumber) < \(secondNumber) < 100")
|
||||
}
|
||||
// 输出 "4 < 42 < 100"
|
||||
// 输出“4 < 42 < 100”
|
||||
|
||||
if let firstNumber = Int("4") {
|
||||
if let secondNumber = Int("42") {
|
||||
@ -629,7 +629,7 @@ if let firstNumber = Int("4") {
|
||||
}
|
||||
}
|
||||
}
|
||||
// 输出 "4 < 42 < 100"
|
||||
// 输出“4 < 42 < 100”
|
||||
```
|
||||
|
||||
> 注意
|
||||
@ -669,7 +669,7 @@ let implicitString: String = assumedString // 不需要感叹号
|
||||
if assumedString != nil {
|
||||
print(assumedString!)
|
||||
}
|
||||
// 输出 "An implicitly unwrapped optional string."
|
||||
// 输出“An implicitly unwrapped optional string.”
|
||||
```
|
||||
|
||||
你也可以在可选绑定中使用隐式解析可选类型来检查并解析它的值:
|
||||
@ -678,7 +678,7 @@ if assumedString != nil {
|
||||
if let definiteString = assumedString {
|
||||
print(definiteString)
|
||||
}
|
||||
// 输出 "An implicitly unwrapped optional string."
|
||||
// 输出“An implicitly unwrapped optional string.”
|
||||
```
|
||||
|
||||
> 注意
|
||||
|
||||
@ -198,7 +198,7 @@ if name == "world" {
|
||||
} else {
|
||||
print("I'm sorry \(name), but I don't recognize you")
|
||||
}
|
||||
// 输出 "hello, world", 因为 `name` 就是等于 "world"
|
||||
// 输出“hello, world", 因为 `name` 就是等于 "world”
|
||||
```
|
||||
|
||||
关于 `if` 语句,请看[控制流](./05_Control_Flow.html)。
|
||||
@ -410,7 +410,7 @@ let allowedEntry = false
|
||||
if !allowedEntry {
|
||||
print("ACCESS DENIED")
|
||||
}
|
||||
// 输出 "ACCESS DENIED"
|
||||
// 输出“ACCESS DENIED”
|
||||
```
|
||||
|
||||
`if !allowedEntry` 语句可以读作「如果非 allowedEntry」,接下一行代码只有在「非 allowedEntry」为 `true`,即 `allowEntry` 为 `false` 时被执行。
|
||||
@ -433,7 +433,7 @@ if enteredDoorCode && passedRetinaScan {
|
||||
} else {
|
||||
print("ACCESS DENIED")
|
||||
}
|
||||
// 输出 "ACCESS DENIED"
|
||||
// 输出“ACCESS DENIED”
|
||||
```
|
||||
|
||||
### 逻辑或运算符
|
||||
@ -452,7 +452,7 @@ if hasDoorKey || knowsOverridePassword {
|
||||
} else {
|
||||
print("ACCESS DENIED")
|
||||
}
|
||||
// 输出 "Welcome!"
|
||||
// 输出“Welcome!”
|
||||
```
|
||||
|
||||
### 逻辑运算符组合计算
|
||||
@ -465,7 +465,7 @@ if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword {
|
||||
} else {
|
||||
print("ACCESS DENIED")
|
||||
}
|
||||
// 输出 "Welcome!"
|
||||
// 输出“Welcome!”
|
||||
```
|
||||
|
||||
这个例子使用了含多个 `&&` 和 `||` 的复合逻辑。但无论怎样,`&&` 和 `||` 始终只能操作两个值。所以这实际是三个简单逻辑连续操作的结果。我们来解读一下:
|
||||
@ -488,7 +488,7 @@ if (enteredDoorCode && passedRetinaScan) || hasDoorKey || knowsOverridePassword
|
||||
} else {
|
||||
print("ACCESS DENIED")
|
||||
}
|
||||
// 输出 "Welcome!"
|
||||
// 输出“Welcome!”
|
||||
```
|
||||
|
||||
这括号使得前两个值被看成整个逻辑表达中独立的一个部分。虽然有括号和没括号的输出结果是一样的,但对于读代码的人来说有括号的代码更清晰。可读性比简洁性更重要,请在可以让你代码变清晰的地方加个括号吧!
|
||||
|
||||
@ -117,7 +117,7 @@ var shoppingList = ["Eggs", "Milk"]
|
||||
|
||||
```swift
|
||||
print("The shopping list contains \(shoppingList.count) items.")
|
||||
// 输出 "The shopping list contains 2 items."(这个数组有2个项)
|
||||
// 输出“The shopping list contains 2 items.”(这个数组有2个项)
|
||||
```
|
||||
|
||||
使用布尔属性 `isEmpty` 作为一个缩写形式去检查 `count` 属性是否为 `0`:
|
||||
@ -151,7 +151,7 @@ shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
|
||||
|
||||
```swift
|
||||
var firstItem = shoppingList[0]
|
||||
// 第一项是 "Eggs"
|
||||
// 第一项是“Eggs”
|
||||
```
|
||||
|
||||
> 注意
|
||||
@ -162,7 +162,7 @@ var firstItem = shoppingList[0]
|
||||
|
||||
```swift
|
||||
shoppingList[0] = "Six eggs"
|
||||
// 其中的第一项现在是 "Six eggs" 而不是 "Eggs"
|
||||
// 其中的第一项现在是“Six eggs”而不是“Eggs”
|
||||
```
|
||||
|
||||
还可以利用下标来一次改变一系列数据值,即使新数据和原有数据的数量是不一样的。下面的例子把 `"Chocolate Spread"`、`"Cheese"` 和 `"Butter"` 替换为 `"Bananas"` 和 `"Apples"`:
|
||||
@ -181,7 +181,7 @@ shoppingList[4...6] = ["Bananas", "Apples"]
|
||||
```swift
|
||||
shoppingList.insert("Maple Syrup", at: 0)
|
||||
// shoppingList 现在有7项
|
||||
// "Maple Syrup" 现在是这个列表中的第一项
|
||||
// 现在是这个列表中的第一项是“Maple Syrup”
|
||||
```
|
||||
|
||||
这次 `insert(_:at:)` 方法调用把值为 `"Maple Syrup"` 的新数据项插入列表的最开始位置,并且使用 `0` 作为索引值。
|
||||
@ -192,7 +192,7 @@ shoppingList.insert("Maple Syrup", at: 0)
|
||||
let mapleSyrup = shoppingList.remove(at: 0)
|
||||
// 索引值为0的数据项被移除
|
||||
// shoppingList 现在只有6项,而且不包括 Maple Syrup
|
||||
// mapleSyrup 常量的值等于被移除数据项的值 "Maple Syrup"
|
||||
// mapleSyrup 常量的值等于被移除数据项“Maple Syrup”的值
|
||||
```
|
||||
|
||||
> 注意
|
||||
@ -203,7 +203,7 @@ let mapleSyrup = shoppingList.remove(at: 0)
|
||||
|
||||
```swift
|
||||
firstItem = shoppingList[0]
|
||||
// firstItem 现在等于 "Six eggs"
|
||||
// firstItem 现在等于“Six eggs”
|
||||
```
|
||||
|
||||
如果我们只想把数组中的最后一项移除,可以使用 `removeLast()` 方法而不是 `remove(at:)` 方法来避免我们需要获取数组的 `count` 属性。就像后者一样,前者也会返回被移除的数据项:
|
||||
@ -212,7 +212,7 @@ firstItem = shoppingList[0]
|
||||
let apples = shoppingList.removeLast()
|
||||
// 数组的最后一项被移除了
|
||||
// shoppingList 现在只有5项,不包括 Apples
|
||||
// apples 常量的值现在等于 "Apples" 字符串
|
||||
// apples 常量的值现在等于“Apples”字符串
|
||||
```
|
||||
|
||||
<a name="iterating_over_an_array"></a>
|
||||
@ -404,9 +404,9 @@ Swift 的 `Set` 类型没有确定的顺序,为了按照特定顺序来遍历
|
||||
for genre in favoriteGenres.sorted() {
|
||||
print("\(genre)")
|
||||
}
|
||||
// prints "Classical"
|
||||
// prints "Hip hop"
|
||||
// prints "Jazz
|
||||
// Classical
|
||||
// Hip hop
|
||||
// Jazz
|
||||
```
|
||||
|
||||
<a name="performing_set_operations"></a>
|
||||
@ -593,7 +593,7 @@ airports["LHR"] = "London Heathrow"
|
||||
if let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB") {
|
||||
print("The old value for DUB was \(oldValue).")
|
||||
}
|
||||
// 输出 "The old value for DUB was Dublin."
|
||||
// 输出“The old value for DUB was Dublin.”
|
||||
```
|
||||
|
||||
我们也可以使用下标语法来在字典中检索特定键对应的值。因为有可能请求的键没有对应的值存在,字典的下标访问会返回对应值的类型的可选值。如果这个字典包含请求键所对应的值,下标会返回一个包含这个存在值的可选值,否则将返回 `nil`:
|
||||
@ -611,7 +611,7 @@ if let airportName = airports["DUB"] {
|
||||
|
||||
```swift
|
||||
airports["APL"] = "Apple Internation"
|
||||
// "Apple Internation" 不是真的 APL 机场,删除它
|
||||
// “Apple Internation”不是真的 APL 机场,删除它
|
||||
airports["APL"] = nil
|
||||
// APL 现在被移除了
|
||||
```
|
||||
@ -624,7 +624,7 @@ if let removedValue = airports.removeValue(forKey: "DUB") {
|
||||
} else {
|
||||
print("The airports dictionary does not contain a value for DUB.")
|
||||
}
|
||||
// prints "The removed airport's name is Dublin Airport."
|
||||
// 打印“The removed airport's name is Dublin Airport.”
|
||||
```
|
||||
|
||||
<a name="iterating_over_a_dictionary"></a>
|
||||
|
||||
@ -65,7 +65,7 @@ for _ in 1...power {
|
||||
answer *= base
|
||||
}
|
||||
print("\(base) to the power of \(power) is \(answer)")
|
||||
// 输出 "3 to the power of 10 is 59049"
|
||||
// 输出“3 to the power of 10 is 59049”
|
||||
```
|
||||
|
||||
这个例子计算 base 这个数的 power 次幂(本例中,是 `3` 的 `10` 次幂),从 `1`(`3` 的 `0` 次幂)开始做 `3` 的乘法, 进行 `10` 次,使用 `1` 到 `10` 的闭区间循环。这个计算并不需要知道每一次循环中计数器具体的值,只需要执行了正确的循环次数即可。下划线符号 `_` (替代循环中的变量)能够忽略当前值,并且不提供循环遍历时对值的访问。
|
||||
@ -243,7 +243,7 @@ var temperatureInFahrenheit = 30
|
||||
if temperatureInFahrenheit <= 32 {
|
||||
print("It's very cold. Consider wearing a scarf.")
|
||||
}
|
||||
// 输出 "It's very cold. Consider wearing a scarf."
|
||||
// 输出“It's very cold. Consider wearing a scarf.”
|
||||
```
|
||||
|
||||
上面的例子会判断温度是否小于等于 32 华氏度(水的冰点)。如果是,则打印一条消息;否则,不打印任何消息,继续执行 `if` 块后面的代码。
|
||||
@ -257,7 +257,7 @@ if temperatureInFahrenheit <= 32 {
|
||||
} else {
|
||||
print("It's not that cold. Wear a t-shirt.")
|
||||
}
|
||||
// 输出 "It's not that cold. Wear a t-shirt."
|
||||
// 输出“It's not that cold. Wear a t-shirt.”
|
||||
```
|
||||
|
||||
显然,这两条分支中总有一条会被执行。由于温度已升至 40 华氏度,不算太冷,没必要再围围巾。因此,`else` 分支就被触发了。
|
||||
@ -273,7 +273,7 @@ if temperatureInFahrenheit <= 32 {
|
||||
} else {
|
||||
print("It's not that cold. Wear a t-shirt.")
|
||||
}
|
||||
// 输出 "It's really warm. Don't forget to wear sunscreen."
|
||||
// 输出“It's really warm. Don't forget to wear sunscreen.”
|
||||
```
|
||||
|
||||
在上面的例子中,额外的 `if` 语句用于判断是不是特别热。而最后的 `else` 语句被保留了下来,用于打印既不冷也不热时的消息。
|
||||
@ -328,7 +328,7 @@ case "z":
|
||||
default:
|
||||
print("Some other character")
|
||||
}
|
||||
// 输出 "The last letter of the alphabet"
|
||||
// 输出“The last letter of the alphabet”
|
||||
```
|
||||
|
||||
在这个例子中,第一个 case 分支用于匹配第一个英文字母 `a`,第二个 case 分支用于匹配最后一个字母 `z`。因为 `switch` 语句必须有一个 case 分支用于覆盖所有可能的字符,而不仅仅是所有的英文字母,所以 switch 语句使用 `default` 分支来匹配除了 `a` 和 `z` 外的所有值,这个分支保证了 swith 语句的完备性。
|
||||
@ -368,7 +368,7 @@ case "a", "A":
|
||||
default:
|
||||
print("Not the letter A")
|
||||
}
|
||||
// 输出 "The letter A
|
||||
// 输出“The letter A”
|
||||
```
|
||||
|
||||
为了可读性,符合匹配可以写成多行形式,详情请参考[复合匹配](#compound_cases)
|
||||
@ -401,7 +401,7 @@ default:
|
||||
naturalCount = "many"
|
||||
}
|
||||
print("There are \(naturalCount) \(countedThings).")
|
||||
// 输出 "There are dozens of moons orbiting Saturn."
|
||||
// 输出“There are dozens of moons orbiting Saturn.”
|
||||
```
|
||||
|
||||
在上例中,`approximateCount` 在一个 `switch` 声明中被评估。每一个 `case` 都与之进行比较。因为 `approximateCount` 落在了 12 到 100 的区间,所以 `naturalCount` 等于 `"dozens of"` 值,并且此后的执行跳出了 `switch` 语句。
|
||||
@ -427,7 +427,7 @@ case (-2...2, -2...2):
|
||||
default:
|
||||
print("\(somePoint) is outside of the box")
|
||||
}
|
||||
// 输出 "(1, 1) is inside the box"
|
||||
// 输出“(1, 1) is inside the box”
|
||||
```
|
||||
|
||||

|
||||
@ -453,7 +453,7 @@ case (0, let y):
|
||||
case let (x, y):
|
||||
print("somewhere else at (\(x), \(y))")
|
||||
}
|
||||
// 输出 "on the x-axis with an x value of 2"
|
||||
// 输出“on the x-axis with an x value of 2”
|
||||
```
|
||||
|
||||

|
||||
@ -483,7 +483,7 @@ case let (x, y) where x == -y:
|
||||
case let (x, y):
|
||||
print("(\(x), \(y)) is just some arbitrary point")
|
||||
}
|
||||
// 输出 "(1, -1) is on the line x == -y"
|
||||
// 输出“(1, -1) is on the line x == -y”
|
||||
```
|
||||
|
||||

|
||||
@ -510,7 +510,7 @@ case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
|
||||
default:
|
||||
print("\(someCharacter) is not a vowel or a consonant")
|
||||
}
|
||||
// 输出 "e is a vowel"
|
||||
// 输出“e is a vowel”
|
||||
```
|
||||
|
||||
这个 `switch` 语句中的第一个 case,匹配了英语中的五个小写元音字母。相似的,第二个 case 匹配了英语中所有的小写辅音字母。最终,`default` 分支匹配了其它所有字符。
|
||||
@ -525,7 +525,7 @@ case (let distance, 0), (0, let distance):
|
||||
default:
|
||||
print("Not on an axis")
|
||||
}
|
||||
// 输出 "On an axis, 9 from the origin"
|
||||
// 输出“On an axis, 9 from the origin”
|
||||
```
|
||||
|
||||
上面的 case 有两个模式:`(let distance, 0)` 匹配了在 x 轴上的值,`(0, let distance)` 匹配了在 y 轴上的值。两个模式都绑定了 `distance`,并且 `distance` 在两种模式下,都是整型——这意味着分支体内的代码,只要 case 匹配,都可以获取到 `distance` 值。
|
||||
@ -562,7 +562,7 @@ for character in puzzleInput {
|
||||
}
|
||||
}
|
||||
print(puzzleOutput)
|
||||
// 输出 "grtmndsthnklk"
|
||||
// 输出“grtmndsthnklk”
|
||||
```
|
||||
|
||||
在上面的代码中,只要匹配到元音字母或者空格字符,就调用 `continue` 语句,使本次循环结束,重新开始下次循环。这种行为使 `switch` 匹配到元音字母和空格字符时不做处理,而不是让每一个匹配到的字符都被打印。
|
||||
@ -610,7 +610,7 @@ if let integerValue = possibleIntegerValue {
|
||||
} else {
|
||||
print("An integer value could not be found for \(numberSymbol).")
|
||||
}
|
||||
// 输出 "The integer value of 三 is 3."
|
||||
// 输出“The integer value of 三 is 3.”
|
||||
```
|
||||
|
||||
这个例子检查 `numberSymbol` 是否是拉丁,阿拉伯,中文或者泰语中的 `1` 到 `4` 之一。如果被匹配到,该 `switch` 分支语句给 `Int?` 类型变量 `possibleIntegerValue` 设置一个整数值。
|
||||
@ -637,7 +637,7 @@ default:
|
||||
description += " an integer."
|
||||
}
|
||||
print(description)
|
||||
// 输出 "The number 5 is a prime number, and also an integer."
|
||||
// 输出“The number 5 is a prime number, and also an integer.”
|
||||
```
|
||||
|
||||
这个例子定义了一个 `String` 类型的变量 `description` 并且给它设置了一个初始值。函数使用 `switch` 逻辑来判断 `integerToDescribe` 变量的值。当 `integerToDescribe` 的值属于列表中的质数之一时,该函数在 `description` 后添加一段文字,来表明这个数字是一个质数。然后它使用 `fallthrough` 关键字来“贯穿”到 `default` 分支中。`default` 分支在 `description` 的最后添加一段额外的文字,至此 `switch` 代码块执行完了。
|
||||
@ -732,19 +732,23 @@ func greet(person: [String: String]) {
|
||||
guard let name = person["name"] else {
|
||||
return
|
||||
}
|
||||
print("Hello \(name)")
|
||||
|
||||
print("Hello \(name)!")
|
||||
|
||||
guard let location = person["location"] else {
|
||||
print("I hope the weather is nice near you.")
|
||||
return
|
||||
}
|
||||
|
||||
print("I hope the weather is nice in \(location).")
|
||||
}
|
||||
greet(["name": "John"])
|
||||
// 输出 "Hello John!"
|
||||
// 输出 "I hope the weather is nice near you."
|
||||
greet(["name": "Jane", "location": "Cupertino"])
|
||||
// 输出 "Hello Jane!"
|
||||
// 输出 "I hope the weather is nice in Cupertino."
|
||||
|
||||
greet(person: ["name": "John"])
|
||||
// 输出“Hello John!”
|
||||
// 输出“I hope the weather is nice near you.”
|
||||
greet(person: ["name": "Jane", "location": "Cupertino"])
|
||||
// 输出“Hello Jane!”
|
||||
// 输出“I hope the weather is nice in Cupertino.”
|
||||
```
|
||||
|
||||
如果 `guard` 语句的条件被满足,则继续执行 `guard` 语句大括号后的代码。将变量或者常量的可选绑定作为 `guard` 语句的条件,都可以保护 `guard` 语句后面的代码。
|
||||
|
||||
@ -92,7 +92,7 @@ manager.data.append("Some more data")
|
||||
```swift
|
||||
print(manager.importer.fileName)
|
||||
// DataImporter 实例的 importer 属性现在被创建了
|
||||
// 输出 "data.txt”
|
||||
// 输出“data.txt”
|
||||
```
|
||||
|
||||
> 注意
|
||||
@ -138,7 +138,7 @@ var square = Rect(origin: Point(x: 0.0, y: 0.0),
|
||||
let initialSquareCenter = square.center
|
||||
square.center = Point(x: 15.0, y: 15.0)
|
||||
print("square.origin is now at (\(square.origin.x), \(square.origin.y))")
|
||||
// 打印 "square.origin is now at (10.0, 10.0)”
|
||||
// 打印“square.origin is now at (10.0, 10.0)”
|
||||
```
|
||||
|
||||
这个例子定义了 3 个结构体来描述几何形状:
|
||||
@ -200,7 +200,7 @@ struct Cuboid {
|
||||
}
|
||||
let fourByFiveByTwo = Cuboid(width: 4.0, height: 5.0, depth: 2.0)
|
||||
print("the volume of fourByFiveByTwo is \(fourByFiveByTwo.volume)")
|
||||
// 打印 "the volume of fourByFiveByTwo is 40.0"
|
||||
// 打印“the volume of fourByFiveByTwo is 40.0”
|
||||
```
|
||||
|
||||
这个例子定义了一个名为 `Cuboid` 的结构体,表示三维空间的立方体,包含 `width`、`height` 和 `depth` 属性。结构体还有一个名为 `volume` 的只读计算属性用来返回立方体的体积。为 `volume` 提供 setter 毫无意义,因为无法确定如何修改 `width`、`height` 和 `depth` 三者的值来匹配新的 `volume`。然而,`Cuboid` 提供一个只读计算属性来让外部用户直接获取体积是很有用的。
|
||||
@ -340,14 +340,14 @@ class SomeClass {
|
||||
|
||||
```swift
|
||||
print(SomeStructure.storedTypeProperty)
|
||||
// 打印 "Some value."
|
||||
// 打印“Some value.”
|
||||
SomeStructure.storedTypeProperty = "Another value."
|
||||
print(SomeStructure.storedTypeProperty)
|
||||
// 打印 "Another value.”
|
||||
// 打印“Another value.”
|
||||
print(SomeEnumeration.computedTypeProperty)
|
||||
// 打印 "6"
|
||||
// 打印“6”
|
||||
print(SomeClass.computedTypeProperty)
|
||||
// 打印 "27"
|
||||
// 打印“27”
|
||||
```
|
||||
|
||||
下面的例子定义了一个结构体,使用两个存储型类型属性来表示两个声道的音量,每个声道具有 `0` 到 `10` 之间的整数音量。
|
||||
@ -404,9 +404,9 @@ var rightChannel = AudioChannel()
|
||||
```swift
|
||||
leftChannel.currentLevel = 7
|
||||
print(leftChannel.currentLevel)
|
||||
// 输出 "7"
|
||||
// 输出“7”
|
||||
print(AudioChannel.maxInputLevelForAllChannels)
|
||||
// 输出 "7"
|
||||
// 输出“7”
|
||||
```
|
||||
|
||||
如果试图将右声道的 `currentLevel` 设置成 `11`,它会被修正到最大值 `10`,同时 `maxInputLevelForAllChannels` 的值也会更新到 `10`:
|
||||
@ -414,7 +414,7 @@ print(AudioChannel.maxInputLevelForAllChannels)
|
||||
```swift
|
||||
rightChannel.currentLevel = 11
|
||||
print(rightChannel.currentLevel)
|
||||
// 输出 "10"
|
||||
// 输出“10”
|
||||
print(AudioChannel.maxInputLevelForAllChannels)
|
||||
// 输出 "10"
|
||||
// 输出“10”
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user