diff --git a/source/chapter3/07_Patterns.md b/source/chapter3/07_Patterns.md index d28428b7..10d87dd4 100755 --- a/source/chapter3/07_Patterns.md +++ b/source/chapter3/07_Patterns.md @@ -1,6 +1,5 @@ -> 翻译:honghaoz - -> 校对:numbbbbb, stanzhai +> 翻译:honghaoz +> 校对:numbbbbb, stanzhai # 模式(Patterns) ----------------- @@ -35,9 +34,11 @@ 通配符模式匹配并忽略任何值,包含一个下划线(_)。当你不关心被匹配的值时,可以使用此模式。例如,下面这段代码进行了`1...3`的循环,并忽略了每次循环的值: - for _ in 1...3 { - // Do something three times. - } +```swift +for _ in 1...3 { + // Do something three times. +} +``` > 通配符模式语法 > *通配符模式* → **_** @@ -47,7 +48,9 @@ 标识符模式匹配任何值,并将匹配的值和一个变量或常量绑定起来。例如,在下面的常量申明中,`someValue`是一个标识符模式,匹配了类型是`Int`的`42`。 - let someValue = 42 +```swift +let someValue = 42 +``` 当匹配成功时,`42`被绑定(赋值)给常量`someValue`。 @@ -63,13 +66,15 @@ 标识符模式包含在值绑定模式中,绑定新的变量或常量到匹配的值。例如,你可以分解一个元组的元素,并把每个元素绑定到相应的标识符模式中。 - let point = (3, 2) - switch point { - // Bind x and y to the elements of point. - case let (x, y): - println("The point is at (\(x), \(y)).") - } - // prints "The point is at (3, 2).” +```swift +let point = (3, 2) +switch point { + // Bind x and y to the elements of point. +case let (x, y): + println("The point is at (\(x), \(y)).") +} +// prints "The point is at (3, 2).” +``` 在上面这个例子中,`let`将元组模式`(x, y)`分配到各个标识符模式。因为这种行为,`switch`语句中`case let (x, y):`和`case (let x, let y):`匹配的值是一样的。 @@ -85,17 +90,21 @@ 当元组模式被用在`for-in`语句或者变量或常量申明时,它可以包含通配符模式,标识符模式或者其他包含这两种模式的模式。例如,下面这段代码是不正确的,因为`(x, 0)`中的元素`0`是一个表达式模式: - let points = [(0, 0), (1, 0), (1, 1), (2, 0), (2, 1)] - // This code isn't valid. - for (x, 0) in points { - /* ... */ - } +```swift +let points = [(0, 0), (1, 0), (1, 1), (2, 0), (2, 1)] +// This code isn't valid. +for (x, 0) in points { + /* ... */ +} +``` 对于只包含一个元素的元组,括号是不起作用的。模式匹配那个单个元素的类型。例如,下面是等效的: - let a = 2 // a: Int = 2 - let (a) = 2 // a: Int = 2 - let (a): Int = 2 // a: Int = 2 +```swift +let a = 2 // a: Int = 2 +let (a) = 2 // a: Int = 2 +let (a): Int = 2 // a: Int = 2 +``` > 元组模式语法 > *元组模式* → **(** [*元组模式元素列表*](..\chapter3\07_Patterns.html#tuple_pattern_element_list) _可选_ **)** @@ -117,8 +126,8 @@ 有两种类型转换模式,`is`模式和`as`模式。这两种模式均只出现在`switch`语句中的`case`标签中。`is`模式和`as`模式有以下形式: - is type - pattern as type +> is `type` +> `pattern` as `type` `is`模式匹配一个值,如果这个值的类型在运行时(runtime)和`is`模式右边的指定类型(或者那个类型的子类)是一致的。`is`模式和`is`操作符一样,它们都进行类型转换,但是抛弃了返回的类型。 @@ -138,33 +147,36 @@ 由表达式模式所代表的表达式用Swift标准库中的`~=`操作符与输入表达式的值进行比较。如果`~=`操作符返回`true`,则匹配成功。默认情况下,`~=`操作符使用`==`操作符来比较两个相同类型的值。它也可以匹配一个整数值与一个`Range`对象中的整数范围,正如下面这个例子所示: - let point = (1, 2) - switch point { - case (0, 0): - println("(0, 0) is at the origin.") - case (-2...2, -2...2): - println("(\(point.0), \(point.1)) is near the origin.") - default: - println("The point is at (\(point.0), \(point.1)).") - } - // prints "(1, 2) is near the origin.” +```swift +let point = (1, 2) +switch point { +case (0, 0): + println("(0, 0) is at the origin.") +case (-2...2, -2...2): + println("(\(point.0), \(point.1)) is near the origin.") +default: + println("The point is at (\(point.0), \(point.1)).") +} +// prints "(1, 2) is near the origin.” +``` 你可以重载`~=`操作符来提供自定义的表达式行为。例如,你可以重写上面的例子,以实现用字符串表达的点来比较`point`表达式。 - // Overload the ~= operator to match a string with an integer - func ~=(pattern: String, value: Int) -> Bool { - return pattern == "\(value)" - } - switch point { - case ("0", "0"): - println("(0, 0) is at the origin.") - case ("-2...2", "-2...2"): - println("(\(point.0), \(point.1)) is near the origin.") - default: - println("The point is at (\(point.0), \(point.1)).") - } - // prints "(1, 2) is near the origin.” +```swift +// Overload the ~= operator to match a string with an integer +func ~=(pattern: String, value: Int) -> Bool { + return pattern == "\(value)" +} +switch point { +case ("0", "0"): + println("(0, 0) is at the origin.") +case ("-2...2", "-2...2"): + println("(\(point.0), \(point.1)) is near the origin.") +default: + println("The point is at (\(point.0), \(point.1)).") +} +// prints "(1, 2) is near the origin.” +``` > 表达式模式语法 -> *表达式模式* → [*表达式*](..\chapter3\04_Expressions.html#expression) - +> *表达式模式* → [*表达式*](..\chapter3\04_Expressions.html#expression) \ No newline at end of file