@ -388,7 +388,7 @@ let someInstance: SomeBaseClass = SomeSubClass()
|
||||
// someInstance 在编译期是 SomeBaseClass 类型,
|
||||
// 但是在运行期则是 SomeSubClass 类型
|
||||
type(of: someInstance).printClassName()
|
||||
// 打印 “SomeSubClass”
|
||||
// 打印“SomeSubClass”
|
||||
```
|
||||
|
||||
更多信息可以查看 Swift 标准库里的 `type(of:)`。
|
||||
|
||||
@ -196,14 +196,14 @@ func f(any: Any) { print("Function for Any") }
|
||||
func f(int: Int) { print("Function for Int") }
|
||||
let x = 10
|
||||
f(x)
|
||||
// 打印 “Function for Int”
|
||||
// 打印“Function for Int”
|
||||
|
||||
let y: Any = x
|
||||
f(y)
|
||||
// 打印 “Function for Any”
|
||||
// 打印“Function for Any”
|
||||
|
||||
f(x as Any)
|
||||
// 打印 “Function for Any”
|
||||
// 打印“Function for Any”
|
||||
```
|
||||
|
||||
桥接可将 Swift 标准库中的类型(例如 `String`)作为一个与之相关的 Foundation 类型(例如 `NSString`)来使用,而不需要新建一个实例。关于桥接的更多信息,请参阅 [*Using Swift with Cocoa and Objective-C (Swift4.1)*](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/index.html#//apple_ref/doc/uid/TP40014216) 中的 [Working with Cocoa Data Types](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html#//apple_ref/doc/uid/TP40014216-CH6)。
|
||||
@ -265,7 +265,7 @@ func logFunctionName(string: String = #function) {
|
||||
func myFunction() {
|
||||
logFunctionName()
|
||||
}
|
||||
myFunction() // 打印 “myFunction()”
|
||||
myFunction() // 打印“myFunction()”
|
||||
```
|
||||
|
||||
数组字面量是值的有序集合,形式如下:
|
||||
@ -441,7 +441,7 @@ let closure = { [a] in
|
||||
a = 10
|
||||
b = 10
|
||||
closure()
|
||||
// 打印 “0 10”
|
||||
// 打印“0 10”
|
||||
```
|
||||
|
||||
在示例中,变量 `b` 只有一个,然而,变量 `a` 有两个,一个在闭包外,一个在闭包内。闭包内的变量 `a` 会在闭包创建时用闭包外的变量 `a` 的值来初始化,除此之外它们并无其他联系。这意味着在闭包创建后,改变某个 `a` 的值都不会对另一个 `a` 的值造成任何影响。与此相反,闭包内外都是同一个变量 `b`,因此在闭包外改变其值,闭包内的值也会受影响。
|
||||
@ -461,7 +461,7 @@ let closure = { [x] in
|
||||
x.value = 10
|
||||
y.value = 10
|
||||
closure()
|
||||
// 打印 “10 10”
|
||||
// 打印“10 10”
|
||||
```
|
||||
|
||||
如果捕获列表中的值是类类型,你可以使用 `weak` 或者 `unowned` 来修饰它,闭包会分别用弱引用和无主引用来捕获该值。
|
||||
@ -698,7 +698,7 @@ class SomeSubClass: SomeSuperClass {
|
||||
let initializer: Int -> String = String.init
|
||||
let oneTwoThree = [1, 2, 3].map(initializer).reduce("", combine: +)
|
||||
print(oneTwoThree)
|
||||
// 打印 “123”
|
||||
// 打印“123”
|
||||
```
|
||||
|
||||
如果通过名字来指定某个类型,可以不用构造器表达式而直接使用类型的构造器。在其他情况下,你必须使用构造器表达式。
|
||||
|
||||
@ -477,9 +477,9 @@ func f() {
|
||||
defer { print("Third") }
|
||||
}
|
||||
f()
|
||||
// 打印 “Third”
|
||||
// 打印 “Second”
|
||||
// 打印 “First”
|
||||
// 打印“Third”
|
||||
// 打印“Second”
|
||||
// 打印“First”
|
||||
```
|
||||
|
||||
`defer` 语句中的语句无法将控制权转移到 `defer` 语句外部。
|
||||
|
||||
@ -166,9 +166,9 @@ let (firstNumber, secondNumber) = (10, 42)
|
||||
|
||||
```swift
|
||||
print("The first number is \(firstNumber).")
|
||||
// 打印 “The first number is 10.”
|
||||
// 打印“The first number is 10.”
|
||||
print("The second number is \(secondNumber).")
|
||||
// 打印 “The second number is 42.”
|
||||
// 打印“The second number is 42.”
|
||||
```
|
||||
|
||||
当常量名称的类型(`:` 类型)可以被推断出时,类型标注在常量声明中是可选的,正如 [类型推断](03_Types.html#type_inference) 中所描述的。
|
||||
@ -479,7 +479,7 @@ var x = 10
|
||||
let f = outer(&x)
|
||||
f()
|
||||
print(x)
|
||||
// 打印 “10”
|
||||
// 打印“10”
|
||||
```
|
||||
|
||||
调用嵌套函数 `inner()` 对 `a` 递增后,`x` 的值并未发生改变,因为 `inner()` 在外层函数 `outer()` 返回后才被调用。若要改变 `x` 的值,必须在 `outer()` 返回前调用 `inner()`。
|
||||
|
||||
@ -93,7 +93,7 @@ switch point {
|
||||
case let (x, y):
|
||||
print("The point is at (\(x), \(y)).")
|
||||
}
|
||||
// 打印 “The point is at (3, 2).”
|
||||
// 打印“The point is at (3, 2).”
|
||||
```
|
||||
|
||||
在上面这个例子中,`let` 会分配到元组模式 `(x, y)` 中的各个标识符模式。因此,`switch` 语句中 `case let (x, y):` 和 `case (let x, let y):` 的匹配效果是一样的。
|
||||
@ -227,7 +227,7 @@ case (-2...2, -2...2):
|
||||
default:
|
||||
print("The point is at (\(point.0), \(point.1)).")
|
||||
}
|
||||
// 打印 “(1, 2) is near the origin.”
|
||||
// 打印“(1, 2) is near the origin.”
|
||||
```
|
||||
|
||||
你可以重载 `~=` 运算符来提供自定义的表达式匹配行为。比如你可以重写上面的例子,将 `point` 表达式与字符串形式表示的点进行比较。
|
||||
@ -244,7 +244,7 @@ case ("0", "0"):
|
||||
default:
|
||||
print("The point is at (\(point.0), \(point.1)).")
|
||||
}
|
||||
// 打印 “The point is at (1, 2).”
|
||||
// 打印“The point is at (1, 2).”
|
||||
```
|
||||
|
||||
> 表达式模式语法
|
||||
|
||||
Reference in New Issue
Block a user