您正在查看: Swift 分类下的文章

The Swift Programming Language 03Language Reference_10Summary of the Grammar

翻译:StanZhai

校对:xielingwang

语法总结


本页包含内容:

  • 语句(Statements)
  • 泛型参数(Generic Parameters and Arguments)
  • 声明(Declarations)
  • 模式(Patterns)
  • 特性(Attributes)
  • 表达式(Expressions)
  • 词法结构(Lexical Structure)
  • 类型(Types)

语句

语句语法
statement → [expression]https://developer.apple.com/library/prerelease/ios/documentation/swift/Conceptual/swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression ; opt
statement → [declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration ; opt
statement → [loop-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement ; opt
statement → [branch-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/branch-statement ; opt
statement → [labeled-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/labeled-statement
statement → [control-transfer-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/control-transfer-statement ; opt
statements → [statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement [statements]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements opt

循环语句语法
loop-statement → [for-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-statement
loop-statement → [for-in-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-in-statement
loop-statement → [while-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/wheetatype类型ile-statement
loop-statement → [do-while-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/do-while-statement

For 循环语法
for-statementfor [for-init]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init opt ; [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression opt ; [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression opt [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block
for-statementfor ( [for-init]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init opt ; [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression opt ; [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression opt ) [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block
for-init → [variable-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration | [expression-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression-list

For-In 循环语法
for-in-statementfor [pattern]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern in [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block

While 循环语法
while-statementwhile [while-condition]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block
while-condition → [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression | [declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration

Do-While 循环语法
do-while-statementdo [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block while [while-condition]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition

分支语句语法
branch-statement → [if-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement
branch-statement → [switch-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement

If 语句语法
if-statementif [if-condition]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-condition [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block [else-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/else-clause opt
if-condition → [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression | [declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration
else-clauseelse [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block | else [if-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement

Switch 语句语法
switch-statementswitch [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression { [switch-cases]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases opt }
switch-cases → [switch-case]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-case [switch-cases]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases opt
switch-case → [case-label]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label [statements]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements | [default-label]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label [statements]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements
switch-case → [case-label]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label ; | [default-label]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label ;
case-labelcase [case-item-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list :
case-item-list → [pattern]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern [guard-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause opt | [pattern]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern [guard-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause opt , [case-item-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list
default-labeldefault :
guard-clausewhere [guard-expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-expression
guard-expression → [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression

带标签的语句语法
labeled-statement → [statement-label]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label [loop-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement | [statement-label]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label [switch-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement
statement-label → [label-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name :
label-name → [identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier

控制传递语句语法
control-transfer-statement → [break-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/break-statement
control-transfer-statement → [continue-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/continue-statement
control-transfer-statement → [fallthrough-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/fallthrough-statement
control-transfer-statement → [return-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/return-statement

Break 语句语法
break-statementbreak [label-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name opt

Continue 语句语法
continue-statementcontinue [label-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name opt

Fallthrough 语句语法
fallthrough-statementfallthrough

Return 语句语法
return-statementreturn [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression opt

泛型参数

泛型形参子句语法
generic-parameter-clause< [generic-parameter-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-list [requirement-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/requirement-clause opt >
generic-parameter-list → [generic-parameter]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter | [generic-parameter]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter , [generic-parameter-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-list
generic-parameter → [type-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-name
generic-parameter → [type-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-name : [type-identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier
generic-parameter → [type-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-name : [protocol-composition-type]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/protocol-composition-type
requirement-clausewhere [requirement-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/requirement-list
requirement-list → [requirement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/requirement | [requirement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/requirement , [requirement-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/requirement-list
requirement → [conformance-requirement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/conformance-requirement | [same-type-requirement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/same-type-requirement
conformance-requirement → [type-identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier : [type-identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier
conformance-requirement → [type-identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier : [protocol-composition-type]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/protocol-composition-type
same-type-requirement → [type-identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier == [type-identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier

泛型实参子句语法
generic-argument-clause< [generic-argument-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-argument-list >
generic-argument-list → [generic-argument]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-argument | [generic-argument]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-argument , [generic-argument-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-argument-list
generic-argument → [type]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type

声明 (Declarations)

声明语法
declaration → [import-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/import-declaration
declaration → [constant-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/constant-declaration
declaration → [variable-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration
declaration → [typealias-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/typealias-declaration
declaration → [function-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-declaration
declaration → [enum-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/enum-declaration
declaration → [struct-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/struct-declaration
declaration → [class-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/class-declaration
declaration → [protocol-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-declaration
declaration → [initializer-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/initializer-declaration
declaration → [deinitializer-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/deinitializer-declaration
declaration → [extension-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/extension-declaration
declaration → [subscript-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/subscript-declaration
declaration → [operator-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/operator-declaration
declarations → [declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration [declarations]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declarations opt
declaration-specifiers → [declaration-specifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration-specifier [declaration-specifiers]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration-specifiers opt
declaration-specifierclass | mutating | nonmutating | override | static | unowned | unowned(safe) | unowned(unsafe) | weak

顶级声明语法
top-level-declaration → [statements]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements opt

代码块语法
code-block{ [statements]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements opt }

Import 声明语法
import-declaration → [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes opt import [import-kind]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/import-kind opt [import-path]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/import-path
import-kindtypealias | struct | class | enum | protocol | var | func
import-path → [import-path-identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/import-path-identifier | [import-path-identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/import-path-identifier . [import-path]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/import-path
import-path-identifier → [identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier | [operator]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator

常数声明语法
constant-declaration → [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes opt [declaration-specifiers]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration-specifiers opt let [pattern-initializer-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/pattern-initializer-list
pattern-initializer-list → [pattern-initializer]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/pattern-initializer | [pattern-initializer]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/pattern-initializer , [pattern-initializer-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/pattern-initializer-list
pattern-initializer → [pattern]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern [initializer]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/initializer opt
initializer= [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression

变量声明语法
variable-declaration → [variable-declaration-head]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration-head [pattern-initializer-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/pattern-initializer-list
variable-declaration → [variable-declaration-head]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration-head [variable-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-name [type-annotation]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-annotation [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block
variable-declaration → [variable-declaration-head]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration-head [variable-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-name [type-annotation]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-annotation [getter-setter-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-setter-block
variable-declaration → [variable-declaration-head]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration-head [variable-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-name [type-annotation]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-annotation [getter-setter-keyword-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-setter-keyword-block
variable-declaration → [variable-declaration-head]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration-head [variable-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-name [type-annotation]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-annotation [initializer]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/initializer opt [willSet-didSet-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/willSet-didSet-block
variable-declaration-head → [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes opt [declaration-specifiers]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration-specifiers opt var
variable-name → [identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier
getter-setter-block{ [getter-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-clause [setter-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/setter-clause opt }
getter-setter-block{ [setter-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/setter-clause [getter-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-clause }
getter-clause → [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes opt get [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block
setter-clause → [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes opt set [setter-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/setter-name opt [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block
setter-name( [identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier )
getter-setter-keyword-block{ [getter-keyword-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-keyword-clause [setter-keyword-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/setter-keyword-clause opt }
getter-setter-keyword-block{ [setter-keyword-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/setter-keyword-clause [getter-keyword-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-keyword-clause }
getter-keyword-clause → [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes opt get
setter-keyword-clause → [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes opt set
willSet-didSet-block{ [willSet-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/willSet-clause [didSet-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/didSet-clause opt }
willSet-didSet-block{ [didSet-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/didSet-clause [willSet-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/willSet-clause }
willSet-clause → [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes opt willSet [setter-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/setter-name opt [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block
didSet-clause → [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes opt didSet [setter-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/setter-name opt [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block

类型别名声明语法
typealias-declaration → [typealias-head]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/typealias-head [typealias-assignment]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/typealias-assignment
typealias-headtypealias [typealias-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/typealias-name
typealias-name → [identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier
typealias-assignment= [type]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type

函数声明语法
function-declaration → [function-head]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-head [function-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-name [generic-parameter-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-clause opt [function-signature]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-signature [function-body]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-body
function-head → [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes opt [declaration-specifiers]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration-specifiers opt func
function-name → [identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier | [operator]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator
function-signature → [parameter-clauses]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-clauses [function-result]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-result opt
function-result-> [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes opt [type]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type
function-body → [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block
parameter-clauses → [parameter-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-clause [parameter-clauses]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-clauses opt
parameter-clause( ) | ( [parameter-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-list ... opt )
parameter-list → [parameter]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter | [parameter]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter , [parameter-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-list
parameterinout opt let opt # opt [parameter-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-name [local-parameter-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/local-parameter-name opt [type-annotation]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-annotation [default-argument-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/default-argument-clause opt
parameterinout opt var # opt [parameter-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-name [local-parameter-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/local-parameter-name opt [type-annotation]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-annotation [default-argument-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/default-argument-clause opt
parameter → [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes opt [type]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type
parameter-name → [identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier | **_** local-parameter-name → [identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier | **_** default-argument-clause= [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression

枚举声明语法
enum-declaration → [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes opt [union-style-enum]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/union-style-enum | [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes opt [raw-value-style-enum]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-style-enum
union-style-enum → [enum-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/enum-name [generic-parameter-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-clause opt { [union-style-enum-members]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/union-style-enum-members opt }
union-style-enum-members → [union-style-enum-member]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/union-style-enum-member [union-style-enum-members]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/union-style-enum-members opt
union-style-enum-member → [declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration | [union-style-enum-case-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/union-style-enum-case-clause
union-style-enum-case-clause → [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes opt case [union-style-enum-case-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/union-style-enum-case-list
union-style-enum-case-list → [union-style-enum-case]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/union-style-enum-case | [union-style-enum-case]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/union-style-enum-case , [union-style-enum-case-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/union-style-enum-case-list
union-style-enum-case → [enum-case-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/enum-case-name [tuple-type]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/tuple-type opt
enum-name → [identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier
enum-case-name → [identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier
raw-value-style-enum → [enum-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/enum-name [generic-parameter-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-clause opt : [type-identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-identifier { [raw-value-style-enum-members]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-style-enum-members opt }
raw-value-style-enum-members → [raw-value-style-enum-member]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-style-enum-member [raw-value-style-enum-members]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-style-enum-members opt
raw-value-style-enum-member → [declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration | [raw-value-style-enum-case-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-style-enum-case-clause
raw-value-style-enum-case-clause → [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes opt case [raw-value-style-enum-case-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-style-enum-case-list
raw-value-style-enum-case-list → [raw-value-style-enum-case]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-style-enum-case | [raw-value-style-enum-case]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-style-enum-case , [raw-value-style-enum-case-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-style-enum-case-list
raw-value-style-enum-case → [enum-case-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/enum-case-name [raw-value-assignment]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/raw-value-assignment opt
raw-value-assignment= [literal]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/literal

结构体声明语法
struct-declaration → [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes opt struct [struct-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/struct-name [generic-parameter-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-clause opt [type-inheritance-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-inheritance-clause opt [struct-body]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/struct-body
struct-name → [identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier
struct-body{ [declarations]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declarations opt }

类声明语法
class-declaration → [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes opt class [class-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/class-name [generic-parameter-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-clause opt [type-inheritance-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-inheritance-clause opt [class-body]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/class-body
class-name → [identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier
class-body{ [declarations]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declarations opt }

协议声明语法
protocol-declaration → [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attributes opt protocol [protocol-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-name [type-inheritance-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-inheritance-clause opt [protocol-body]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-body
protocol-name → [identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier
protocol-body{ [protocol-member-declarations]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-member-declarations opt }
protocol-member-declaration → [protocol-property-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-property-declaration
protocol-member-declaration → [protocol-method-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-method-declaration
protocol-member-declaration → [protocol-initializer-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-initializer-declaration
protocol-member-declaration → [protocol-subscript-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-subscript-declaration
protocol-member-declaration → [protocol-associated-type-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-associated-type-declaration
protocol-member-declarations → [protocol-member-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-member-declaration [protocol-member-declarations]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/protocol-member-declarations opt

协议属性声明语法
protocol-property-declaration → [variable-declaration-head]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration-head [variable-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-name [type-annotation]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-annotation [getter-setter-keyword-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-setter-keyword-block

协议方法声明语法
protocol-method-declaration → [function-head]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-head [function-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-name [generic-parameter-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-clause opt [function-signature]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/function-signature

协议构造函数声明语法
protocol-initializer-declaration → [initializer-head]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/initializer-head [generic-parameter-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-clause opt [parameter-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-clause

协议附属脚本声明语法
protocol-subscript-declaration → [subscript-head]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/subscript-head [subscript-result]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/subscript-result [getter-setter-keyword-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/getter-setter-keyword-block

协议关联类型声明语法
protocol-associated-type-declaration → [typealias-head]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/typealias-head [type-inheritance-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/swift/grammar/type-inheritance-clause opt [typealias-assignment]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/typealias-assignment opt

构造函数声明语法
initializer-declaration → [initializer-head]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/initializer-head [generic-parameter-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GenericParametersAndArguments.html#//apple_ref/swift/grammar/generic-parameter-clause opt [parameter-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/parameter-clause [initializer-body]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/initializer-body
initializer-head → [attributes]https://developer.apple.com/library/prerelease/ios/doc

The Swift Programming Language 03Language Reference_07Attributes

属性

属性 提供了更多关于声明和类型的信息。在 swift 中有两种类型的属性,一种用于声明,一种用于类型。例如,required 属性-当用于类的指定或者便利初始化声明时-指明每个子类都必须实现它的初始化函数。noreturn 属性-当用于函数或者方法的类型时-指明函数或者方法无需返回值。

你可以用字符 @ 加上属性名和属性参数来指定一个属性:

@属性名

@属性名(属性参数)

含有参数的声明属性可以为属性指定更多的信息,可以用于特殊的声明。这些属性参数 被包含在圆括号里,参数的格式由属性决定。

声明属性

声明属性只能用于声明,当然,你也可以使用 noreturn 属性作为函数或者方法的类型

####assignment

此属性可用于修饰重载复合赋值运算符的函数。这个重载复合赋值运算符的函数必须用 inout 来标记初始输入参数。assignment 属性示例参见[复合赋值运算符]https://developer.apple.com/library/prerelease/ios/documentation/swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-XID_45。

####class_protocol

此属性可用于定义类类型协议。

如果你使用 objc 属性的协议, 那么这个协议就隐式含有 class_protocol 属性,你无需显式标记 class_protocol 属性。

####exported

此属性可用于内部声明,可以将当前模块的内部模块、子模块或者声明暴露给外部其他模块。如果另一个模块引用了当前模块,那么这个模块就可以访问当前模块中暴露出来的部分。

####final

此属性可用于修饰类或者类的属性、方法或成员下标运算符。用于一个类的时候,表明这个类是不能被继承的。用于类的属性、方法或成员下标运算符的时候,表明这个类的这些成员函数不能在任何子类中重写。

####lazy

此属性可用于修饰类或者结构体中的存储变量属性,表明这个属性在第一次被访问时,其初始值最多只能被计算和存储一次。 lazy 属性示例参见[惰性存储属性]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html#//apple_ref/doc/uid/TP40014097-CH14-XID_326。

####noreturn

此属性用于函数或者方法的声明,表明其函数或者方法相应的类型T@noreturn T。当一个函数或者方法无需返回其调用者时,你可以用这个属性来修饰其类型。

你可以重写没有标示noreturn属性的函数或者方法。也就是说,你不能够重写有noreturn属性的函数或者方法。当你实现此类型的协议方法时,也有相似的规则。

####NSCopying

此属性可用于修饰类中的存储变量属性。被修饰的这个属性的赋值函数是由这个属性值的拷贝组成-由copyWithZone方法返回-而不是这个属性本身的值。此属性类型必须符合NSCopying协议。

NSCopying属性类似于Objective-C中的copy属性。

####NSManaged

用于修饰类中的存储变量属性,此类继承于NSManagedObject,表明这个属性的存储和实现是由Core Data基于相关的实体描述实时动态提供的。

####objc

此属性可用于能用Objective-C表示的任何声明中-例如,非嵌套的类、协议、类和协议的属性和方法(包括取值函数和赋值函数)、初始化函数、析构函数以及下标运算符。objc属性告诉编译器此声明在Objective-C代码中可用。

如果你使用objc属性修饰类或者协议,它会显式的应用于这个类或者协议中的所有成员。当一个类继承于标注objc属性的另一类时,编译器会显式的为这个类添加objc属性。标注objc属性的协议不能够继承于不含有objc属性的协议。

objc属性可以接受由标识符组成的单个属性参数。当你希望暴露给Objective-C的部分是一个不同的名字时,你可以使用objc属性。你可以使用这个参数去命名类、协议、方法、取值函数、赋值函数以及初始化函数。下面的示例就是ExampleClassenabled属性的取值函数,它暴露给Objective-C代码的是isEnabled,而不是这个属性的原名。

python">@objc
class ExampleClass {
    var enabled: Bool {
    @objc(isEnabled) get {
        // Return the appropriate value
    }
    }
}

####optional

此属性可用于协议的属性、方法或者成员下标运算符,用来表明实现那些成员函数时,此类型的不是必需实现的。

optional属性只能用于标注objc属性的协议。因此,包含可选成员的协议只有类类型适用。更多的关于怎样使用optional属性,以及怎样访问可选协议成员的指导-例如,当你不确定它们是否实现了此类型时-参见[可选协议需求]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-XID_365。

####required

此属性用于类的指定或者便利初始化函数时,表明这个类的每个子类都必须实现这个初始化函数。

需求的指定初始化函数必须被显式的包含。当子类直接实现所有超类的指定初始化函数时(或者子类使用便利初始化函数重写了指定初始化函数时),需求的便利初始化函数必须被显式的包含或者继承。

####使用Interface Builder声明属性

Interface Builder属性就是使用Interface Builder声明属性以与Xcode同步。Swift提供了如下几种Interface Builder属性:IBActionIBdesignableIBInspectable以及IBOutlet。这些属性理论上与Objective-C中相应的属性一样。

IBOutletIBInspectable属性可用于类的属性声明,IBAction属性可用于类的方法声明,IBDesignable属性可用于类的声明。

##类型属性

类型属性可以只用于类型。当然noreturn属性也可以用于函数或者方法的声明。

####auto_closure

此属性用于延迟表达式的赋值计算,将表达式自动封装成一个无参数的闭包。此属性还可作为函数或者方法的类型,此类型无参数并且其返回的是表达式类型。auto_closure属性示例参见[函数类型]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/doc/uid/TP40014097-CH31-XID_1005。

####noreturn

此属性用于函数或者方法时表明此函数或者方法无返回值。你也可以用此属性标记函数或者方法的声明,以表明其函数或者方法相应的类型T@noreturn T

属性语法

attribute → @­[attribute-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attribute-argument-clause [attribute-argument-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/attribute-argument-clause opt

attribute-name[identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier

attribute-argument-clause → ([balanced-tokens­]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/balanced-tokens opt)

attributes[attribute­]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/balanced-tokens [attributes]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/balanced-tokens­ opt­

balanced-tokens[balanced-token]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/balanced-tokens ­[balanced-tokens­]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/balanced-tokens opt­

balanced-token → (­[balanced-tokens­]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/balanced-tokens opt­)­

balanced-token → [[balanced-tokens­]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/balanced-tokens opt­]­

balanced-token → {[balanced-tokens­]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/swift/grammar/balanced-tokens opt­­}­

balanced-token → 任意标识符,关键字,常量,或运算符

balanced-token → 任意的标点符号 (­, )­, [­, ]­, {­, 或 }­

The Swift Programming Language 03Language Reference_08Patterns

模式

模式 代表了单值或组合值的结构。例如,元组(1,2) 的结构是用逗号分隔两个元素的列表。因为模式代表一种值的结构,而不是一个任何一个特殊的值,你可以用多种值与之匹配。举个例子,模式 (x,y) 可以匹配元组 (1,2) 以及其他任何两个元素的元组。除了将一个值与模式匹配,你还可以提取组合值的部分或全部,然后把各部分绑定到常量或变量上。

swift 中,模式出现在变量和常量的声明中(在它们的左边),在 for-in 语句和 switch 语句(在他们的 case 标签)中。虽然模式可能出现在 switch 语句的任何一个 case 标签中,但是在其他情况下,只有通配符模式,标识符模式,以及包含这两种模式的模式才能够出现。

你可以为通配符模式,标识符模式,以及元组模式指定一个类型注释,以约束模式只匹配符合特定类型的值。

模式的语法

pattern → identifier-pattern­type-annotation­opt­

pattern → value-binding-pattern­

pattern → tuple-pattern­type-annotation­opt
­
pattern → enum-case-pattern­

pattern → type-casting-pattern­

pattern → expression-pattern

通配符模式

通配符模式 匹配并忽略任何值,包括下划线(_)。当你不关心被匹配的值时,可以使用通配符模式。比如说,下面的代码在闭区间 1...3 中迭代,并忽略每次迭代时区间的当前值:

swift">for _ in 1...3 {
    // 执行三次
}

通配符模式的语法

wildcard-pattern → _­

标识符模式

标识符模式 匹配任何值,并将匹配的值绑定到一个变量或常量上。例如,在下面的常量声明中,someValue 是一个匹配了 Int 型的值为 42 的标识符模式。

let someValue = 42

当匹配成功时,42 被绑定(赋值)到常量 someValue

当一个变量或常量声明左侧的模式是标识符模式时,该标识符模式是隐式的值绑定模式。

标识符模式的语法

identifier-pattern → identifier­

值绑定模式

值绑定模式 绑定匹配的值到变量或常量。绑定匹配值到常量名时,值绑定模式以关键字 let 开头;而绑定值到变量名时,以关键字 var 开头。

标识符模式包含于值绑定模式中绑定新命名的变量或常量到它们匹配的值上。例如,你可以分解一个元组的元素并把每个元素的值绑定到相应的标识符模式。

let point = (3, 2)
switch point {
    // 绑定 x 和 y 到 point 中的元素 
case let (x, y):
    println("The point is at (\(x), \(y)).")
}
// 打印 "The point is at (3, 2)."

在上面的例子中,let 将元组模式 (x, y) 的元素分配到各个标识符模式。由于这个行为,switch 语句 case let (x, y):case (let x, let y): 匹配的值是一样的。

值绑定模式的语法

value-binding-pattern → var­pattern­ let­pattern­

元组模式

元组模式 是包含零个或多个模式由一个逗号分隔的列表,包含在括号中。元组模式匹配相应元组类型的值。

你可以通过使用类型注释来限制一个元组模式,以匹配特定种类的元组类型。例如,在常量声明 let (x, y): (Int, Int) = (1, 2) 中的元组模式 (x, y): (Int, Int),只匹配两个元素都是 Int 型的元组。如果只需要限制元组模式中的一些元素,直接向这些单个元素提供类型注释即可。例如,在 let (x: String, y) 中的元组模式匹配任意包含两个元素且第一个元素是 String 型的元组类型。

当元组模式被用作 for-in 语句或者变量或常量申明的模式时,它只能包含通配符模式,标识符模式或者其他包含两者的模式。例如,下面的代码是不合法的,因为 (x, 0) 中的元素 是一个表达式模式:

let points = [(0, 0), (1, 0), (1, 1), (2, 0), (2, 1)]
// 以下代码不合法
for (x, 0) in points {
    /* ... */
}

围绕只包含单个元素的元组模式的括号是不起作用的。模式匹配单个元素类型的值。例如,下面的式子是等价的:

let a = 2        // a: Int = 2
let (a) = 2      // a: Int = 2
let (a): Int = 2 // a: Int = 2

元组模式的语法

tuple-pattern → (tuple-pattern-element-list opt)

tuple-pattern-element-list → tuple-pattern-element | tuple-pattern-element, tuple-pattern-element-list

tuple-pattern-element → pattern

枚举实例模式

枚举实例模式 匹配现有的枚举类型的实例。枚举实例模式只出现在 switch 语句的 case 标签中。

如果你准备匹配的枚举实例有任意关联的值,则相应的枚举实例模式必须指定一个包含每个关联值元素的元组模式。使用 switch 语句来匹配包含关联值枚举实例的例子,请参阅 [Associated Values]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html#//apple_ref/doc/uid/TP40014097-CH12-XID_189。

枚举实例模式的语法

enum-case-pattern → type-identifier opt . enum-case-name tuple-pattern opt

类型转换模式

类型转换模式有两种,is 模式和 as 模式。两种模式都只出现在 switch 语句中的 case 标签中。is 模式和 as 模式有下面的形式:

is type
pattern as type

如果一个值的类型在运行时和 is 模式右侧的指定类型或者指定类型的子类是相同的,那么 is 模式会匹配该值。is 模式的行为就像 is 操作符一样,他们都进行类型转换,但是舍弃了返回类型。

如果一个值的类型在运行时(runtime)和 as 模式右边的指定类型或者指定类型的子类是相同的,那么 as 模式会匹配该值。如果匹配成功,匹配值的类型将被转换成 as 模式左边指定的模式。

使用 switch 语句匹配使用 is 模式和 as 模式的值的例子,请参阅 [Type Casting for Any and AnyObject]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html#//apple_ref/doc/uid/TP40014097-CH22-XID_448。

类型转换模式的语法

type-casting-pattern → is-pattern­ as-pattern­

is-pattern → is­type­

as-pattern → pattern­as­type­

表达式模式

表达式模式 代表了一个表达式的值。表达式模式只出现在 switch 语句中的 case 标签中。

由表达式模式所代表的表达式通过使用 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)).")
}
// 输出 "(1, 2) is near the origin."

你可以重载 ~= 操作符来提供自定义表达式的匹配行为。例如,你可以重写上面的例子以比较 point 表达式和点的字符串表示形式。

// 重载 `~=` 操作符以匹配值为整形的字符串
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)).")
}

// 输出 "(1, 2) is near the origin."

表达式模式的语法

expression-pattern → expression­

The Swift Programming Language 03Language Reference_09Generic Parameters and Arguments

翻译:fd5788

校对:yankuangshi

泛型参数


本页包含内容:

  • 泛型形参语句
  • 泛型实参语句

本节涉及泛型类型、泛型函数以及泛型构造器的参数,包括形参和实参。声明泛型类型、函数或构造器时,须指定相应的类型参数。类型参数相当于一个占位符,当实例化泛型类型、调用泛型函数或泛型构造器时,就用具体的类型实参替代之。

关于 swift 语言的泛型概述,见泛型(第二部分第22章)。

泛型形参语句

泛型形参语句指定泛型类型或函数的类型形参,以及这些参数的关联约束和要求。泛型形参语句用尖括号(<>)包住,并且有以下两种形式:

<generic parameter list>
<generic parameter list where requirements >

泛型形参列表中泛型形参用逗号分开,每一个采用以下形式:

type parameter : constrain

泛型形参由两部分组成:类型形参及其后的可选约束。类型形参只是占位符类型(如T,U,V,KeyType,ValueType等)的名字而已。你可以在泛型类型、函数的其余部分或者构造器声明,以及函数或构造器的签名中使用它。

约束用于指明该类型形参继承自某个类或者遵守某个协议或协议的一部分。例如,在下面的泛型中,泛型形参T: Comparable表示任何用于替代类型形参T的类型实参必须满足Comparable协议。

func simpleMin<T: COmparable>(x: T, y: T) -> T {
    if x < y {
        return y
    }
    return x
}

如,IntDouble均满足Comparable协议,该函数接受任何一种类型。与泛型类型相反,调用泛型函数或构造器时不需要指定泛型实参语句。类型实参由传递给函数或构造器的实参推断而出。

simpleMin(17, 42) // T is inferred to be Int
simpleMin(3.14159, 2.71828) // T is inferred to be Double

Where语句

要想对类型形参及其关联类型指定额外要求,可以在泛型形参列表之后添加where语句。where语句由关键字where及其后的用逗号分割的多个要求组成。

where语句中的要求用于指明该类型形参继承自某个类或遵守某个协议或协议的一部分。尽管where语句有助于表达类型形参上的简单约束(如T: Comparable等同于T where T: Comparable,等等),但是依然可以用来对类型形参及其关联约束提供更复杂的约束。如,<T where T: C, T: P>表示泛型类型T继承自类C且遵守协议P

如上所述,可以强制约束类型形参的关联类型遵守某个协议。<T: Generator where T.Element: Equatable>表示T遵守Generator协议,而且T的关联类型T.Element遵守Eauatable协议(T有关联类型是因为Generator声明了Element,而T遵守Generator协议)。

也可以用操作符==来指定两个类型等效的要求。例如,有这样一个约束:TU遵守Generator协议,同时要求它们的关联类型等同,可以这样来表达:<T: Generator, U: Generator where T.Element == U.Element>

当然,替代类型形参的类型实参必须满足所有类型形参所要求的约束和要求。

泛型函数或构造器可以重载,但在泛型形参语句中的类型形参必须有不同的约束或要求,抑或二者皆不同。当调用重载的泛型函数或构造器时,编译器会用这些约束来决定调用哪个重载函数或构造器。

泛型类可以生成一个子类,但是这个子类也必须是泛型类。

Grammar of a generic parameter clause

parameter-clause → <­generic-parameter-list­requirement-clause >

­generic-parameter-list → generic-parameter­ generic-parameter­,­generic-parameter-list ­

generic-parameter → type-name­

generic-parameter → type-name­:­type-identifier­

generic-parameter → type-name­:­protocol-composition-type­

requirement-clause → where­requirement-list­

requirement-list → requirement­ requirement­,­requirement-list­

requirement → conformance-requirement­ same-type-requirement­

conformance-requirement → type-identifier­:­type-identifier­

conformance-requirement → type-identifier­:­protocol-composition-type­

same-type-requirement → type-identifier­==­type-identifier

泛型实参语句

泛型实参语句指定泛型类型的类型实参。泛型实参语句用尖括号(<>)包住,形式如下:

< generic argument list >

泛型实参列表中类型实参有逗号分开。类型实参是实际具体类型的名字,用来替代泛型类型的泛型形参语句中的相应的类型形参。从而得到泛型类型的一个特化版本。如,swift标准库的泛型字典类型定义如下:

  struct Dictionary<KeyTypel: Hashable, ValueType>: Collection,
DictionaryLiteralConvertible {
    /* .. */
}

泛型Dictionary类型的特化版本,Dictionary<String, Int>就是用具体的StringInt类型替代泛型类型KeyType: HashableValueType产生的。每一个类型实参必须满足它所替代的泛型形参的所有约束,包括任何where语句所指定的额外的要求。上面的例子中,类型形参KeyType要求满足Hashable协议,因此String也必须满足Hashable协议。

可以用本身就是泛型类型的特化版本的类型实参替代类型形参(假设已满足合适的约束和要求)。例如,为了生成一个元素类型是整型数组的数组,可以用数组的特化版本Array<Int>替代泛型类型Array<T>的类型形参T来实现。

let arrayOfArrays: Array<Array<Int>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

如泛型形参语句所述,不能用泛型实参语句来指定泛型函数或构造器的类型实参。

Grammar of a generic argument clause

generic-argument-clause → <­generic-argument-list­>­

generic-argument-list → generic-argument­ generic-argument­,­generic-argument-list­

generic-argument → type

==============================================
上篇:模式

下篇:语法总结

The Swift Programming Language 03Language Reference_05Statements

翻译:coverxit

校对:numbbbbb

语句


本页包含内容:

  • 循环语句
  • 分支语句
  • 带标签的语句
  • 控制传递语句

swift 中,有两种类型的语句:简单语句和控制流语句。简单语句是最常见的,用于构造表达式和声明。控制流语句则用于控制程序执行的流程,swift 中有三种类型的控制流语句:循环语句、分支语句和控制传递语句。

循环语句用于重复执行代码块;分支语句用于执行满足特定条件的代码块;控制传递语句则用于修改代码的执行顺序。在稍后的叙述中,将会详细地介绍每一种类型的控制流语句。

是否将分号(;)添加到语句的结尾处是可选的。但若要在同一行内写多条独立语句,请务必使用分号。

GRAMMAR OF A STATEMENT

statement → [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression; opt

statement → [declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration; opt

statement → [loop-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement; opt

statement → [branch-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/branch-statement; opt

statement → [labeled-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/labeled-statement

statement → [control-transfer-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/control-transfer-statement; opt

statement → [statment]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement [statements]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements; opt

循环语句

取决于特定的循环条件,循环语句允许重复执行代码块。Swift 提供四种类型的循环语句:for语句、for-in语句、while语句和do-while语句。

通过break语句和continue语句可以改变循环语句的控制流。有关这两条语句,请参考Break 语句和Continue 语句。

GRAMMAR OF A LOOP STATEMENT

loop-statement → [for-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-statement

loop-statement → [for-in-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-in-statement

loop-statement → [while-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-statement

loop-statement → [do-while-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/do-while-statement

For 语句

for语句允许在重复执行代码块的同时,递增一个计数器。

for语句的形式如下:

for `initialzation`; `condition`; `increment` {
    `statements`
}

initialzationconditionincrement之间的分号,以及包围循环体statements的大括号都是不可省略的。

for语句的执行流程如下:

  1. initialzation只会被执行一次,通常用于声明和初始化在接下来的循环中需要使用的变量。

  2. 计算condition表达式:
    如果为真(true),statements将会被执行,然后转到第3步。如果为假(false),statementsincrement都不会被执行,for至此执行完毕。

  3. 计算increment表达式,然后转到第2步。

定义在initialzation中的变量仅在for语句的作用域以内有效。condition表达式的值的类型必须遵循LogicValue协议。

GRAMMAR OF A FOR STATEMENT

for-statementfor [for-init]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init opt ; [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression opt ; [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression opt [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block

for-statementfor ( [for-init]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/for-init opt ; [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression opt ; [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression opt ) [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block

for-statement → [variable-declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/variable-declaration | [expression-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression-list

For-In 语句

for-in语句允许在重复执行代码块的同时,迭代集合(或遵循Sequence协议的任意类型)中的每一项。

for-in语句的形式如下:

for `item` in `collection` {
    `statements`
}

for-in语句在循环开始前会调用collection表达式的generate方法来获取一个生成器类型(这是一个遵循Generator协议的类型)的值。接下来循环开始,调用collection表达式的next方法。如果其返回值不是None,它将会被赋给item,然后执行statements,执行完毕后回到循环开始处;否则,将不会赋值给item也不会执行statementsfor-in至此执行完毕。

GRAMMAR OF A FOR-IN STATEMENT

for-in-statementfor [pattern]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern in [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block

While 语句

while语句允许重复执行代码块。

while语句的形式如下:

while `condition` {
    `statements`
}

while语句的执行流程如下:

  1. 计算condition表达式:
    如果为真(true),转到第2步。如果为假(false),while至此执行完毕。

  2. 执行statements,然后转到第1步。

由于condition的值在statements执行前就已计算出,因此while语句中的statements可能会被执行若干次,也可能不会被执行。

condition表达式的值的类型必须遵循LogicValue协议。同时,condition表达式也可以使用可选绑定,请参考可选绑定待添加链接

GRAMMAR OF A WHILE STATEMENT

while-statementwhile [while-condition]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block

while-condition → [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression | [declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration

Do-While 语句

do-while语句允许代码块被执行一次或多次。

do-while语句的形式如下:

do {
    `statements`
} while `condition`

do-while语句的执行流程如下:

  1. 执行statements,然后转到第2步。

  2. 计算condition表达式:
    如果为真(true),转到第1步。如果为假(false),do-while至此执行完毕。

由于condition表达式的值是在statements表达式执行后才计算出,因此do-while语句中的statements至少会被执行一次。

condition表达式的值的类型必须遵循LogicValue协议。同时,condition表达式也可以使用可选绑定,请参考可选绑定待添加链接

GRAMMAR OF A DO-WHILE STATEMENT

do-while-statementdo [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block while [while-condition]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/while-condition

分支语句

取决于一个或者多个条件的值,分支语句允许程序执行指定部分的代码。显然,分支语句中条件的值将会决定如何分支以及执行哪一块代码。Swift 提供两种类型的分支语句:if语句和switch语句。

switch语句中的控制流可以用break语句修改,请参考Break 语句。

GRAMMAR OF A BRANCH STATEMENT

branch-statement → [if-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement

branch-statement → [switch-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement

If 语句

取决于一个或多个条件的值,if语句将决定执行哪一块代码。

if语句有两种标准形式,在这两种形式里都必须有大括号。

第一种形式是当且仅当条件为真时执行代码,像下面这样:

if `condition` {
    `statements`
}

第二种形式是在第一种形式的基础上添加else语句,当只有一个else语句时,像下面这样:

if `condition` {
    `statements to execute if condition is true`
} else {
    `statements to execute if condition is false`
}

同时,else语句也可包含if语句,从而形成一条链来测试更多的条件,像下面这样:

if `condition 1` {
    `statements to execute if condition 1 is true`
} else if `condition 2` {
    `statements to execute if condition 2 is true`
}
else {
    `statements to execute if both conditions are false`
}

if语句中条件的值的类型必须遵循LogicValue协议。同时,条件也可以使用可选绑定,请参考可选绑定待添加链接

GRAMMAR OF AN IF STATEMENT

if-statementif [if-condition]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-condition [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block [else-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/else-clause opt

if-condition → [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression | [declaration]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/declaration

else-clauseelse [code-block]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/swift/grammar/code-block | else [if-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/if-statement opt

Switch 语句

取决于switch语句的控制表达式(control expression),switch语句将决定执行哪一块代码。

switch语句的形式如下:

switch `control expression` {
    case `pattern 1`:
        `statements`
    case `pattern 2` where `condition`:
        `statements`
    case `pattern 3` where `condition`,
    `pattern 4` where `condition`:
        `statements`
    default:
        `statements`
}

switch语句的控制表达式(control expression)会首先被计算,然后与每一个case的模式(pattern)进行匹配。如果匹配成功,程序将会执行对应的case块里的statements。另外,每一个case块都不能为空,也就是说在每一个case块中至少有一条语句。如果你不想在匹配到的case块中执行代码,只需在块里写一条break语句即可。

可以用作控制表达式的值是十分灵活的,除了标量类型(scalar types,如IntCharacter)外,你可以使用任何类型的值,包括浮点数、字符串、元组、自定义类的实例和可选(optional)类型,甚至是枚举类型中的成员值和指定的范围(range)等。关于在switch语句中使用这些类型,请参考控制流一章的Switch。

你可以在模式后面添加一个起保护作用的表达式(guard expression)。起保护作用的表达式是这样构成的:关键字where后面跟着一个作为额外测试条件的表达式。因此,当且仅当控制表达式匹配一个case的某个模式且起保护作用的表达式为真时,对应case块中的statements才会被执行。在下面的例子中,控制表达式只会匹配含两个相等元素的元组,如(1, 1)

case let (x, y) where x == y:
}

正如上面这个例子,也可以在模式中使用let(或var)语句来绑定常量(或变量)。这些常量(或变量)可以在其对应的起保护作用的表达式和其对应的case块里的代码中引用。但是,如果case中有多个模式匹配控制表达式,那么这些模式都不能绑定常量(或变量)。

switch语句也可以包含默认(default)块,只有其它case块都无法匹配控制表达式时,默认块中的代码才会被执行。一个switch语句只能有一个默认块,而且必须在switch语句的最后面。

尽管模式匹配操作实际的执行顺序,特别是模式的计算顺序是不可知的,但是 Swift 规定switch语句中的模式匹配的顺序和书写源代码的顺序保持一致。因此,当多个模式含有相同的值且能够匹配控制表达式时,程序只会执行源代码中第一个匹配的case块中的代码。

Switch 语句必须是完备的

在 Swift 中,switch语句中控制表达式的每一个可能的值都必须至少有一个case块与之对应。在某些情况下(例如,表达式的类型是Int),你可以使用默认块满足该要求。

不存在隐式的贯穿(fall through)

当匹配的case块中的代码执行完毕后,程序会终止switch语句,而不会继续执行下一个case块。这就意味着,如果你想执行下一个case块,需要显式地在你需要的case块里使用fallthrough语句。关于fallthrough语句的更多信息,请参考Fallthrough 语句。

GRAMMAR OF A SWITCH STATEMENT

switch-statementswitch [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression { [switch-cases]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases opt }

switch-cases → [switch-case]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-case [switch-cases]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-cases opt

switch-case → [case-label]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label [statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements | [default-label]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label [statements]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statements

switch-case → [case-label]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-label ; | [default-label]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/default-label ;

case-labelcase [case-item-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list :

case-item-list → [pattern]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern [guard-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause opt | [pattern]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/pattern [guard-clause]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-clause opt, [case-item-list]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-item-list

default-labeldefault :

guard-clausewhere [guard-expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-expression

guard-expression → [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression


带标签的语句

你可以在循环语句或switch语句前面加上标签,它由标签名和紧随其后的冒号(:)组成。在breakcontinue后面跟上标签名可以显式地在循环语句或switch语句中更改控制流,把控制权传递给指定标签标记的语句。关于这两条语句用法,请参考Break 语句和Continue 语句。

标签的作用域是该标签所标记的语句之后的所有语句。你可以不使用带标签的语句,但只要使用它,标签名就必唯一。

关于使用带标签的语句的例子,请参考控制流一章的带标签的语句待添加链接

GRAMMAR OF A LABELED STATEMENT

labeled-statement → [statement-label]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label [loop-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/loop-statement | [statement-label]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/statement-label [switch-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/switch-statement

statement-label → [label-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name :

label-name → [identifier]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier

控制传递语句

通过无条件地把控制权从一片代码传递到另一片代码,控制传递语句能够改变代码执行的顺序。Swift 提供四种类型的控制传递语句:break语句、continue语句、fallthrough语句和return语句。

GRAMMAR OF A CONTROL TRANSER STATEMENT

control-transfer-statement → [break-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/break-statement

control-transfer-statement → [continue-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/continue-statement

control-transfer-statement → [fallthrough-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/fallthrough-statement

control-transfer-statement → [return-statement]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/return-statement

Break 语句

break语句用于终止循环或switch语句的执行。使用break语句时,可以只写break这个关键词,也可以在break后面跟上标签名(label name),像下面这样:

break
break `label name`

break语句后面带标签名时,可用于终止由这个标签标记的循环或switch语句的执行。

而当只写break时,则会终止switch语句或上下文中包含break语句的最内层循环的执行。

在这两种情况下,控制权都会被传递给循环或switch语句外面的第一行语句。

关于使用break语句的例子,请参考控制流一章的Break待添加链接和带标签的语句待添加链接

GRAMMAR OF A BREAK STATEMENT

break-statementbreak [label-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name opt

Continue 语句

continue语句用于终止循环中当前迭代的执行,但不会终止该循环的执行。使用continue语句时,可以只写continue这个关键词,也可以在continue后面跟上标签名(label name),像下面这样:

continue
continue `label name`

continue语句后面带标签名时,可用于终止由这个标签标记的循环中当前迭代的执行。

而当只写break时,可用于终止上下文中包含continue语句的最内层循环中当前迭代的执行。

在这两种情况下,控制权都会被传递给循环外面的第一行语句。

for语句中,continue语句执行后,increment表达式还是会被计算,这是因为每次循环体执行完毕后increment表达式都会被计算。

关于使用continue语句的例子,请参考控制流一章的Continue待添加链接和带标签的语句待添加链接

GRAMMAR OF A CONTINUE STATEMENT

continue-statementcontinue [label-name]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/label-name opt

Fallthrough 语句

fallthrough语句用于在switch语句中传递控制权。fallthrough语句会把控制权从switch语句中的一个case传递给下一个case。这种传递是无条件的,即使下一个case的值与switch语句的控制表达式的值不匹配。

fallthrough语句可出现在switch语句中的任意case里,但不能出现在最后一个case块中。同时,fallthrough语句也不能把控制权传递给使用了可选绑定的case块。

关于在switch语句中使用fallthrough语句的例子,请参考控制流一章的控制传递语句待添加链接

GRAMMAR OF A FALLTHROUGH STATEMENT

continue-statementfallthrough

Return 语句

return语句用于在函数或方法的实现中将控制权传递给调用者,接着程序将会从调用者的位置继续向下执行。

使用return语句时,可以只写return这个关键词,也可以在return后面跟上表达式,像下面这样:

return
return `expression`

return语句后面带表达式时,表达式的值将会返回给调用者。如果表达式值的类型与调用者期望的类型不匹配,Swift 则会在返回表达式的值之前将表达式值的类型转换为调用者期望的类型。

而当只写return时,仅仅是将控制权从该函数或方法传递给调用者,而不返回一个值。(这就是说,该函数或方法的返回类型为Void()

GRAMMAR OF A RETURN STATEMENT

return-statementreturn [expression]https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression opt