modify pattern code style
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
> 翻译:honghaoz
|
> 翻译:honghaoz
|
||||||
|
|
||||||
> 校对:numbbbbb
|
> 校对:numbbbbb、stanzhai
|
||||||
|
|
||||||
# 模式(Patterns)
|
# 模式(Patterns)
|
||||||
-----------------
|
-----------------
|
||||||
@ -11,7 +11,7 @@
|
|||||||
- [标识符模式(Identifier Pattern)](#identifier_pattern)
|
- [标识符模式(Identifier Pattern)](#identifier_pattern)
|
||||||
- [值绑定模式(Value-Binding Pattern)](#value-binding_pattern)
|
- [值绑定模式(Value-Binding Pattern)](#value-binding_pattern)
|
||||||
- [元组模式(Tuple Pattern)](#tuple_pattern)
|
- [元组模式(Tuple Pattern)](#tuple_pattern)
|
||||||
- [枚举案例模式(Enumeration Case Pattern)](#enumeration_case_pattern)
|
- [枚举用例模式(Enumeration Case Pattern)](#enumeration_case_pattern)
|
||||||
- [类型转换模式(Type-Casting Patterns)](#type-casting_patterns)
|
- [类型转换模式(Type-Casting Patterns)](#type-casting_patterns)
|
||||||
- [表达式模式(Expression Pattern)](#expression_pattern)
|
- [表达式模式(Expression Pattern)](#expression_pattern)
|
||||||
|
|
||||||
@ -21,21 +21,14 @@
|
|||||||
|
|
||||||
你可以为通配符模式(wildcard pattern),标识符模式(identifier pattern)和元组模式(tuple pattern)指定类型注释,用来限制这种模式只匹配某种类型的值。
|
你可以为通配符模式(wildcard pattern),标识符模式(identifier pattern)和元组模式(tuple pattern)指定类型注释,用来限制这种模式只匹配某种类型的值。
|
||||||
|
|
||||||
> 模式的语法:
|
> 模式(Patterns) 语法
|
||||||
>
|
> *模式* → [*通配符模式*](..\chapter3\07_Patterns.html#wildcard_pattern) [*类型注解*](..\chapter3\03_Types.html#type_annotation) _可选_
|
||||||
> pattern → wildcard-patterntype-annotationopt
|
> *模式* → [*标识符模式*](..\chapter3\07_Patterns.html#identifier_pattern) [*类型注解*](..\chapter3\03_Types.html#type_annotati(Value Binding)on) _可选_
|
||||||
>
|
> *模式* → [*值绑定模式*](..\chapter3\07_Patterns.html#value_binding_pattern)
|
||||||
> pattern → identifier-patterntype-annotationopt
|
> *模式* → [*元组模式*](..\chapter3\07_Patterns.html#tuple_pattern) [*类型注解*](..\chapter3\03_Types.html#type_annotation) _可选_
|
||||||
>
|
> *模式* → [*enum-case-pattern*](..\chapter3\07_Patterns.html#enum_case_pattern)
|
||||||
> pattern → value-binding-pattern
|
> *模式* → [*type-casting-pattern*](..\chapter3\07_Patterns.html#type_casting_pattern)
|
||||||
>
|
> *模式* → [*表达式模式*](..\chapter3\07_Patterns.html#expression_pattern)
|
||||||
> pattern → tuple-patterntype-annotationopt
|
|
||||||
>
|
|
||||||
> pattern → enum-case-pattern
|
|
||||||
>
|
|
||||||
> pattern → type-casting-pattern
|
|
||||||
>
|
|
||||||
> pattern → expression-pattern
|
|
||||||
|
|
||||||
<a name="wildcard_pattern"></a>
|
<a name="wildcard_pattern"></a>
|
||||||
## 通配符模式(Wildcard Pattern)
|
## 通配符模式(Wildcard Pattern)
|
||||||
@ -46,9 +39,8 @@
|
|||||||
// Do something three times.
|
// Do something three times.
|
||||||
}
|
}
|
||||||
|
|
||||||
> 通配符模式的语法:
|
> 通配符模式语法
|
||||||
>
|
> *通配符模式* → **_**
|
||||||
> wildcard-pattern → _
|
|
||||||
|
|
||||||
<a name="identifier_pattern"></a>
|
<a name="identifier_pattern"></a>
|
||||||
## 标识符模式(Identifier Pattern)
|
## 标识符模式(Identifier Pattern)
|
||||||
@ -61,9 +53,8 @@
|
|||||||
|
|
||||||
当一个变量或常量申明的左边是标识符模式时,此时,标识符模式是隐式的值绑定模式(value-binding pattern)。
|
当一个变量或常量申明的左边是标识符模式时,此时,标识符模式是隐式的值绑定模式(value-binding pattern)。
|
||||||
|
|
||||||
> 标识符模式的语法:
|
> 标识符模式语法
|
||||||
>
|
> *标识符模式* → [*标识符*](LexicalStructure.html#identifier)
|
||||||
> identifier-pattern → identifier
|
|
||||||
|
|
||||||
<a name="value-binding_pattern"></a>
|
<a name="value-binding_pattern"></a>
|
||||||
## 值绑定模式(Value-Binding Pattern)
|
## 值绑定模式(Value-Binding Pattern)
|
||||||
@ -82,9 +73,8 @@
|
|||||||
|
|
||||||
在上面这个例子中,`let`将元组模式`(x, y)`分配到各个标识符模式。因为这种行为,`switch`语句中`case let (x, y):`和`case (let x, let y):`匹配的值是一样的。
|
在上面这个例子中,`let`将元组模式`(x, y)`分配到各个标识符模式。因为这种行为,`switch`语句中`case let (x, y):`和`case (let x, let y):`匹配的值是一样的。
|
||||||
|
|
||||||
> 值绑定模式的语法:
|
> 值绑定(Value Binding)模式语法
|
||||||
>
|
> *值绑定模式* → **var** [*模式*](..\chapter3\07_Patterns.html#pattern) | **let** [*模式*](..\chapter3\07_Patterns.html#pattern)
|
||||||
> value-binding-pattern → var pattern | let pattern
|
|
||||||
|
|
||||||
<a name="tuple_pattern"></a>
|
<a name="tuple_pattern"></a>
|
||||||
## 元组模式(Tuple Pattern)
|
## 元组模式(Tuple Pattern)
|
||||||
@ -107,24 +97,20 @@
|
|||||||
let (a) = 2 // a: Int = 2
|
let (a) = 2 // a: Int = 2
|
||||||
let (a): Int = 2 // a: Int = 2
|
let (a): Int = 2 // a: Int = 2
|
||||||
|
|
||||||
> 元组模式的语法:
|
> 元组模式语法
|
||||||
>
|
> *元组模式* → **(** [*元组模式元素列表*](..\chapter3\07_Patterns.html#tuple_pattern_element_list) _可选_ **)**
|
||||||
> tuple-pattern → (tuple-pattern-element-list opt)
|
> *元组模式元素列表* → [*元组模式元素*](..\chapter3\07_Patterns.html#tuple_pattern_element) | [*元组模式元素*](..\chapter3\07_Patterns.html#tuple_pattern_element) **,** [*元组模式元素列表*](..\chapter3\07_Patterns.html#tuple_pattern_element_list)
|
||||||
>
|
> *元组模式元素* → [*模式*](..\chapter3\07_Patterns.html#pattern)
|
||||||
> tuple-pattern-element-list → tuple-pattern-element | tuple-pattern-element, tuple-pattern-element-list
|
|
||||||
>
|
|
||||||
> tuple-pattern-element → pattern
|
|
||||||
|
|
||||||
<a name="enumeration_case_pattern"></a>
|
<a name="enumeration_case_pattern"></a>
|
||||||
## 枚举案例模式(Enumeration Case Pattern)
|
## 枚举用例模式(Enumeration Case Pattern)
|
||||||
|
|
||||||
枚举案例模式匹配现有的枚举类型的某种案例。枚举案例模式仅在`switch`语句中的`case`标签中出现。
|
枚举用例模式匹配现有的枚举类型的某种用例。枚举用例模式仅在`switch`语句中的`case`标签中出现。
|
||||||
|
|
||||||
如果你准备匹配的枚举案例有任何关联的值,则相应的枚举案例模式必须指定一个包含每个关联值元素的元组模式。关于使用`switch`语句来匹配包含关联值枚举案例的例子,请参阅`Associated Values`.
|
如果你准备匹配的枚举用例有任何关联的值,则相应的枚举用例模式必须指定一个包含每个关联值元素的元组模式。关于使用`switch`语句来匹配包含关联值枚举用例的例子,请参阅`Associated Values`.
|
||||||
|
|
||||||
> 枚举案例模式的语法:
|
> 枚举用例模式语法
|
||||||
>
|
> *enum-case-pattern* → [*类型标识*](..\chapter3\03_Types.html#type_identifier) _可选_ **.** [*枚举的case名*](..\chapter3\05_Declarations.html#enum_case_name) [*元组模式*](..\chapter3\07_Patterns.html#tuple_pattern) _可选_
|
||||||
> enum-case-pattern → type-identifier opt . enum-case-name tuple-pattern opt
|
|
||||||
|
|
||||||
<a name="type-casting_patterns"></a>
|
<a name="type-casting_patterns"></a>
|
||||||
## 类型转换模式(Type-Casting Patterns)
|
## 类型转换模式(Type-Casting Patterns)
|
||||||
@ -140,13 +126,10 @@
|
|||||||
|
|
||||||
关于使用`switch`语句来匹配`is`模式和`as`模式值的例子,请参阅`Type Casting for Any and AnyObject`。
|
关于使用`switch`语句来匹配`is`模式和`as`模式值的例子,请参阅`Type Casting for Any and AnyObject`。
|
||||||
|
|
||||||
> 类型转换模式的语法:
|
> 类型转换模式语法
|
||||||
>
|
> *type-casting-pattern* → [*is模式*](..\chapter3\07_Patterns.html#is_pattern) | [*as模式*](..\chapter3\07_Patterns.html#as_pattern)
|
||||||
> type-casting-pattern → is-pattern as-pattern
|
> *is模式* → **is** [*类型*](..\chapter3\03_Types.html#type)
|
||||||
>
|
> *as模式* → [*模式*](..\chapter3\07_Patterns.html#pattern) **as** [*类型*](..\chapter3\03_Types.html#type)
|
||||||
> is-pattern → istype
|
|
||||||
>
|
|
||||||
> as-pattern → patternastype
|
|
||||||
|
|
||||||
<a name="expression_pattern"></a>
|
<a name="expression_pattern"></a>
|
||||||
## 表达式模式(Expression Pattern)
|
## 表达式模式(Expression Pattern)
|
||||||
@ -182,7 +165,6 @@
|
|||||||
}
|
}
|
||||||
// prints "(1, 2) is near the origin.”
|
// prints "(1, 2) is near the origin.”
|
||||||
|
|
||||||
> 表达式模式的语法:
|
> 表达式模式语法
|
||||||
>
|
> *表达式模式* → [*表达式*](..\chapter3\04_Expressions.html#expression)
|
||||||
> expression-pattern → expression
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user