Swift 5.0 日本語化計画 : Swift 5.0


文法のまとめ


語彙の構造


空白の文法

whitespacewhitespace-item ­whitespace opt
­ whitespace-itemline-break
­ whitespace-itemcomment­
whitespace-itemmultiline-comment
whitespace-item → U+0000, U+0009, U+000B, U+000C, or U+0020

line-break → U+000A
line-break → U+000D
line-break → U+000D followed by U+000A

comment → // ­comment-text ­line-break
­ multiline-comment → /* multiline-comment-text */

­ comment-textcomment-text-item comment-text­ opt­
comment-text-item → Any Unicode scalar value except U+000A or U+000D

multiline-comment-textmultiline-comment-text-item­ multiline-comment-text opt
­ multiline-comment-text-itemmultiline-comment
­ multiline-comment-text-itemcomment-text-item
multiline-comment-text-item → Any Unicode scalar value except /*­ or */



識別子の文法

identifieridentifier-head­ identifier-characters­ ­opt­
identifier → ` identifier-head­ identifier-characters ­opt `
identifierimplicit-parameter-name­
identifier-listidentifieridentifier­ , ­identifier-list

identifier-headUpper- or lowercase letter A through Z
identifier-head → _
­ identifier-head → U+00A8, U+00AA, U+00AD, U+00AF, U+00B2–U+00B5, or U+00B7–U+00BA
identifier-head → U+00BC–U+00BE, U+00C0–U+00D6, U+00D8–U+00F6, or U+00F8–U+00FF
identifier-head → U+0100–U+02FF, U+0370–U+167F, U+1681–U+180D, or U+180F–U+1DBF
identifier-head → U+1E00–U+1FFF
identifier-head → U+200B–U+200D, U+202A–U+202E, U+203F–U+2040, U+2054, or U+2060–U+206F
identifier-head → U+2070–U+20CF, U+2100–U+218F, U+2460–U+24FF, or U+2776–U+2793
identifier-head → U+2C00–U+2DFF or U+2E80–U+2FFF
identifier-head → U+3004–U+3007, U+3021–U+302F, U+3031–U+303F, or U+3040–U+D7FF
identifier-head → U+F900–U+FD3D, U+FD40–U+FDCF, U+FDF0–U+FE1F, or U+FE30–U+FE44
identifier-head → U+FE47–U+FFFD
identifier-head → U+10000–U+1FFFD, U+20000–U+2FFFD, U+30000–U+3FFFD, or U+40000–U+4FFFD
identifier-head → U+50000–U+5FFFD, U+60000–U+6FFFD, U+70000–U+7FFFD, or U+80000–U+8FFFD
identifier-head → U+90000–U+9FFFD, U+A0000–U+AFFFD, U+B0000–U+BFFFD, or U+C0000–U+CFFFD
identifier-head → U+D0000–U+DFFFD or U+E0000–U+EFFFD

identifier-character → Digit 0 through 9
identifier-character → U+0300–U+036F, U+1DC0–U+1DFF, U+20D0–U+20FF, or U+FE20–U+FE2F
identifier-characteridentifier-head
­ identifier-charactersidentifier-character ­identifier-characters ­opt
­
implicit-parameter-name$ ­decimal-digits­



リテラルの文法

literal numeric-literal | string-literal | boolean-literal | nil-literal
numeric-literal → - opt integer-literal- opt floating-point-literal
boolean-literal true­ | false­
nil-literal nil



­
整数リテラルの文法

integer-literalbinary-literal
integer-literaloctal-literal
­ integer-literaldecimal-literal
­ integer-literalhexadecimal-literal
­
binary-literal0b ­binary-digit ­binary-literal-characters ­opt
binary-digit → Digit 0 or 1
binary-literal-characterbinary-digit |­ _
­ binary-literal-charactersbinary-literal-character­ binary-literal-characters ­opt

octal-literal0o ­octal-digit ­octal-literal-characters ­opt
octal-digit → Digit 0 through 7
octal-literal-characteroctal-digit­ | _
­ octal-literal-charactersoctal-literal-character ­octal-literal-characters ­opt

decimal-literaldecimal-digit­ decimal-literal-characters ­opt
decimal-digit → Digit 0 through 9
decimal-digitsdecimal-digit ­decimal-digits ­opt
­ decimal-literal-characterdecimal-digit |­ _
­ decimal-literal-charactersdecimal-literal-character­ decimal-literal-characters ­opt

hexadecimal-literal0x ­hexadecimal-digit ­hexadecimal-literal-characters ­opt
­ hexadecimal-digit → Digit 0 through 9, a through f, or A through F
hexadecimal-literal-characterhexadecimal-digit |­ _
hexadecimal-literal-charactershexadecimal-literal-character ­hexadecimal-literal-characters ­opt



浮動小数点リテラルの文法

floating-point-literaldecimal-literal­ decimal-fraction ­opt ­decimal-exponent ­opt
floating-point-literalhexadecimal-literal ­hexadecimal-fraction ­opt ­hexadecimal-exponent

decimal-fraction. ­decimal-literal
decimal-exponentfloating-point-e ­sign­ ­opt ­decimal-literal

hexadecimal-fraction. ­hexadecimal-digit ­hexadecimal-literal-characters ­opt
hexadecimal-exponentfloating-point-p ­sign ­opt ­decimal-literal

floating-point-ee­ | E
­ floating-point-ppP
sign+-­



文字列リテラルの文法

string-literalstatic-string-literal | interpolated-string-literal

static-string-literal → " ­quoted-text­ opt ­"
static-string-literal → """­multiline-quoted-text ­opt"""­

quoted-textquoted-text-item ­quoted-text opt
quoted-text-itemescaped-character
­ quoted-text-item → Any Unicode scalar value except ", \­, U+000A, or U+000D

multiline-quoted-textmultiline-quoted-text-item ­multiline-quoted-text ­opt
multiline-quoted-text-itemescaped-character­
multiline-quoted-text-item → Any Unicode scalar value except \
multiline-quoted-text-itemescaped-newline

interpolated-string-literal → " ­interpolated-text­ opt "
interpolated-string-literal → """­ multiline-interpolated-text ­opt­"""

­ interpolated-textinterpolated-text-item ­interpolated-text ­opt
interpolated-text-item\(expression) | quoted-text-item

multiline-interpolated-textmultiline-interpolated-text-item ­multiline-interpolated-text ­opt­
multiline-interpolated-text-item\expression­)­ | multiline-quoted-text-item

escaped-character\0\\­ | \t ­| \n | \r | \"\'
­ escaped-character\u ­{ ­unicode-scalar-digits­}
unicode-scalar-digits → Between one and eight hexadecimal digits

escaped-newline\ ­whitespace ­opt ­line-break



演算子の文法

operatoroperator-head ­operator-characters ­opt
operatordot-operator-head ­dot-operator-characters ­opt

operator-head/=-+!* ­| % | >&|^~ | ­?
­ operator-head → U+00A1–U+00A7
operator-head → U+00A9 or U+00AB
operator-head → U+00AC or U+00AE
operator-head → U+00B0–U+00B1
operator-head → U+00B6, U+00BB, U+00BF, U+00D7, or U+00F7
operator-head → U+2016–U+2017
operator-head → U+2020–U+2027
operator-head → U+2030–U+203E
operator-head → U+2041–U+2053
operator-head → U+2055–U+205E
operator-head → U+2190–U+23FF
operator-head → U+2500–U+2775
operator-head → U+2794–U+2BFF
operator-head → U+2E00–U+2E7F
operator-head → U+3001–U+3003
operator-head → U+3008–U+3020
operator-head → U+3030

operator-characteroperator-head
operator-character → U+0300–U+036F
operator-character → U+1DC0–U+1DFF
operator-character → U+20D0–U+20FF
operator-character → U+FE00–U+FE0F
operator-character → U+FE20–U+FE2F
operator-character → U+E0100–U+E01EF
operator-charactersoperator-character ­operator-characters ­opt

dot-operator-head → .
­ dot-operator-character → .­ | operator-character
dot-operator-charactersdot-operator-character ­dot-operator-characters ­opt

binary-operatoroperator
prefix-operatoroperator
postfix-operatoroperator




型の文法

typearray-type
typedictionary-type
typefunction-type
typetype-identifier
type →­ tuple-type
typeoptional-type
typeimplicitly-unwrapped-optional-type
typeprotocol-composition-type
typemetatype-type­
typeAny
typeSelf
type → ( type )



型の注釈の文法

type-annotation: attributes ­opt inout ­opt ­type



型識別子の文法

type-identifiertype-name generic-argument-clause ­opt | type-name generic-argument-clause ­opt ­. ­type-identifier
type-nameidentifier



タプル型の文法

tuple-type → ( ) | ( tuple-type-element , ­tuple-type-element-list­ )
tuple-type-element-listtuple-type-element | tuple-type-element ­, ­tuple-type-element-list
tuple-type-elementelement-name ­type-annotation | type
element-nameidentifier




関数型の文法

function-typeattributes ­opt ­function-type-argument-clause ­throws ­opt ­-> ­type
function-typeattributes ­opt ­­function-type-argument-clause ­rethrows ­-> ­type

function-type-argument-clause → (­ )­
function-type-argument-clause → ( function-type-argument-list ­... ­opt )
­
function-type-argument-listfunction-type-argument | function-type-argument ­, function-type-argument-list
­ function-type-argumentattributes ­opt ­inout ­opt ­type | argument-label ­type-annotation­
argument-labelidentifier



配列型の文法

array-type → [ ­type ­]



辞書型の文法

dictionary-type → [ ­type­ : type ­]­



Optional の型の文法

optional-typetype ­?



暗黙に開封された optional の型の文法

implicitly-unwrapped-optional-typetype ­!

­

Protocol を構成する型の文法

protocol-composition-typetype-identifier & ­protocol-composition-continuation
protocol-composition-continuationtype-identifier ­| protocol-composition-type



メタタイプ型の文法

metatype-typetype ­. ­Typetype ­. ­Protocol



型の継承句の文法

type-inheritance-clause: ­type-inheritance-list
type-inheritance-listtype-identifiertype-identifier ­, ­type-inheritance-list
­



式の文法

expressiontry-operator opt ­prefix-expression ­binary-expressions ­opt
expression-listexpressionexpression ­, ­expression-list



接頭辞の式の文法

prefix-expressionprefix-operator ­opt ­postfix-expression
prefix-expressionin-out-expression
in-out-expression identifier



try 式の文法

try-operatortry | try ­? | try ­!



二項式の文法

binary-expressionbinary-operator prefix-expression
­ binary-expressionassignment-operator ­try-operator opt prefix-expression
­ binary-expressionconditional-operator ­try-operator opt ­ prefix-expression
­ binary-expressiontype-casting-operator
­ binary-expressionsbinary-expression ­binary-expressions opt



代入演算子の文法

assignment-operator=



条件演算子の文法

conditional-operator? ­expression ­:



型キャスト演算子の文法

type-casting-operatoris type
type-casting-operatoras type
type-casting-operatoras ­? type
type-casting-operatoras ­! type



一次式の文法

primary-expressionidentifier generic-argument-clause opt
primary-expressionliteral-expression
­ primary-expressionself-expression
primary-expressionsuperclass-expression
primary-expressionclosure-expression
primary-expressionparenthesized-expression
primary-expressiontuple-expression
primary-expressionimplicit-member-expression
primary-expressionwildcard-expression
primary-expressionkey-path-expression­
primary-expressionselector-expression­
primary-expressionkey-path-string-expression­



リテラル式の文法

literal-expressionliteral
literal-expressionarray-literaldictionary-literal­ | playground-literal
literal-expression#file­ | #line­ | #column | #function | #dsohandle

array-literal[ ­array-literal-items opt ­]
array-literal-itemsarray-literal-item ­, opt­ | array-literal-item ­, ­array-literal-items
array-literal-itemexpression­

dictionary-literal[ ­dictionary-literal-items ­] | [­:­]
­ dictionary-literal-itemsdictionary-literal-item­ , optdictionary-literal-item ­, dictionary-literal-items
­ dictionary-literal-itemexpression­ ­: expression­

playground-literal#colorLiteral ( ­red ­: expression­ , ­green­ : ­expression­ ­, ­blue : expression­ ­,­ alpha ­:­ expression­­ )
playground-literal#fileLiteral ­( ­resourceName­ : ­expression­­­ )
playground-literal#imageLiteral ­( ­resourceName­ : ­expression­ )



Self 式の文法

self-expressionself | self-method-expression | self-subscript-expression | self-initializer-expression­

self-method-expressionself ­. identifier
self-subscript-expressionself ­[ function-call-argument-list ­]
­ self-initializer-expressionself ­. ­init



スーパークラス式の文法

superclass-expressionsuperclass-method-expressionsuperclass-subscript-expression­ | superclass-initializer-expression­

superclass-method-expressionsuper ­. ­identifier
­ superclass-subscript-expressionsuper ­[ function-call-argument-list ­]
­ superclass-initializer-expressionsuper ­. ­init



クロージャ式の文法

closure-expression{ ­closure-signature ­opt statements opt ­}

closure-signaturecapture-list opt closure-parameter-clause throws opt function-result ­opt ­in
closure-signaturecapture-list ­in

closure-parameter-clause(­) | (­closure-parameter-list­ | identifier-list
closure-parameter-listclosure-parameter | closure-parameter ­, closure-parameter-list
closure-parameterclosure-parameter-name­ type-annotation opt
closure-parameterclosure-parameter-name­ type-annotation ...­
closure-parameter-nameidentifier­

capture-list capture-list-items ­]
capture-list-itemscapture-list-item | capture-list-item ­, capture-list-items
­ capture-list-itemcapture-specifier ­opt ­ expression
capture-specifierweak­ | unowned | unowned(safe) | unowned(unsafe)



暗黙のメンバ式の文法

implicit-member-expression → . ­identifier



括弧で囲まれた式の文法

parenthesized-expression( ­expression ­)



タプル式の文法


tuple-expression( ) | ( ­tuple-element , ­tuple-element-list ­)
tuple-element-listtuple-element | tuple-element , ­tuple-element-list
tuple-elementexpression­ | identifier­ : expression­



ワイルドカードの文法

wildcard-expression → _



キーパス式の文法

key-path-expression\­ type opt ­. ­key-path-components
key-path-componentskey-path-component­ | key-path-component­ . key-path-components
­ key-path-componentidentifier key-path-postfixes ­opt­ | key-path-postfixes

key-path-postfixeskey-path-postfix ­key-path-postfixes ­opt
­ key-path-postfix?! ­| self | [ ­ function-call-argument-list­ ]



セレクタ式の文法

selector-expression#selector ­( expression ­)
selector-expression#selector ­( ­getter : expression ­)
selector-expression#selector ­( ­setter : expression )



キーパス文字列式の文法

key-path-string-expression#keyPath ( expression ­)



­
接尾辞の式の文法

postfix-expressionprimary-expression
postfix-expressionpostfix-expression ­postfix-operator
postfix-expressionfunction-call-expression
postfix-expressioninitializer-expression
postfix-expressionexplicit-member-expression
postfix-expressionpostfix-self-expression
postfix-expressionsubscript-expression
postfix-expressionforced-value-expression
postfix-expressionoptional-chaining-expression­



関数呼び出し式の文法

function-call-expressionpostfix-expression ­function-call-argument-clause
­ function-call-expressionpostfix-expression ­function-call-argument-clause opt trailing-closure

function-call-argument-clause( ­) | ( ­function-call-argument-list ­)
function-call-argument-listfunction-call-argument | function-call-argument , function-call-argument-list
function-call-argumentexpression | identifier­ :­ expression
function-call-argumentoperator | identifier­ : operator

trailing-closureclosure-expression



イニシャライザ式の文法

initializer-expressionpostfix-expression­. ­init
initializer-expressionpostfix-expression­. ­init ( argument-names )



明示的なメンバ式の文法

explicit-member-expressionpostfix-expression ­. ­decimal-digits
explicit-member-expressionpostfix-expression ­. identifier generic-argument-clause ­opt
explicit-member-expressionpostfix-expression . identifier­ ­( ­argument-names ­)

argument-namesargument-name ­argument-names ­opt
argument-nameidentifier­ :



self 式の文法

postfix-self-expressionpostfix-expression ­­. self



サブスクリプト式の文法

subscript-expressionpostfix-expression ­[ ­ function-call-argument-list ]



強制値の式の文法

forced-value-expressionpostfix-expression­ !



optional の連鎖式

optional-chaining-expressionpostfix-expression ­?




文の文法

statementexpression ­; ­opt
statementdeclaration ­; ­opt
statementloop-statement ­; ­opt
­ statementbranch-statement ­; ­opt
statementlabeled-statement ­; ­opt
statementcontrol-transfer-statement ­; ­opt
statementdefer-statement ­; ­opt
statementdo-statement­ ­: ­opt
statementcompiler-control-statement
­ statementsstatement ­statements opt



ループ文の文法

loop-statementfor-in-statement
loop-statementwhile-statement
loop-statementrepeat-while-statement



for-in 文の文法

for-in-statementfor case ­opt pattern ­in ­expression where-clause ­opt code-block



while 文の文法

while-statementwhile condition-list code-block

condition-listcondition | condition ­, ­condition-list
conditionexpression­ | availability-condition ­| case-condition | optional-binding-condition­

case-conditioncase pattern ­initializer­
optional-binding-conditionlet ­pattern­ initializer | var pattern initializer­



repeat-while 文の文法

repeat-while-statementrepeat code-block ­while expression­



分岐文の文法

branch-statementif-statement
branch-statementguard-statement
­ branch-statementswitch-statement



­
if 文の文法

if-statementif condition-list ­code-block else-clause opt
else-clauseelse­ code-blockelse ­if-statement



guard 文の文法

guard-statementguard ­condition-list else ­code-block

­


switch 文の文法

switch-statementswitch ­expression ­{­switch-cases ­opt ­}
­ switch-casesswitch-case switch-cases ­opt
switch-casecase-label ­statements
switch-casedefault-label statements
switch-caseconditional-switch-case

case-labelattributes ­opt case ­case-item-list ­:
case-item-listpattern ­where-clause ­optpattern ­where-clause ­opt ­, case-item-list
­ default-labelattributes ­opt default ­:

where-clausewhere ­where-expression
where-expressionexpression

conditional-switch-caseswitch-if-directive-clause switch-elseif-directive-clauses ­opt switch-else-directive-clause ­opt endif-directive
switch-if-directive-clauseif-directive compilation-condition switch-cases ­opt
switch-elseif-directive-clauseselseif-directive-clause switch-elseif-directive-clauses ­opt
switch-elseif-directive-clauseelseif-directive compilation-condition switch-cases ­opt
switch-else-directive-clauseelse-directive switch-cases ­opt


ラベル付き文の文法

labeled-statementstatement-label ­loop-statement
labeled-statementstatement-label ­if-statement
labeled-statementstatement-label ­switch-statement
labeled-statementstatement-label ­do-statement­

statement-labellabel-name ­:
label-nameidentifier­



制御転送文の文法

control-transfer-statementbreak-statement
control-transfer-statementcontinue-statement
control-transfer-statementfallthrough-statement
control-transfer-statementreturn-statement
control-transfer-statementthrow-statement



break 文の文法

break-statementbreak ­label-name ­opt



continue 文の文法

continue-statementcontinue ­label-name ­opt



fallthrough 文の文法

fallthrough-statementfallthrough



return 文の文法

return-statementreturn ­expression ­opt



throw 文の文法

throw-statementthrow­ expression



defer 文の文法

defer-statementdefer ­­code-block



do 文の文法

do-statementdo ­code-block ­catch-clauses ­opt
catch-clausescatch-clause ­catch-clauses ­opt
catch-clausecatch pattern ­opt where-clause­ opt­ code-block



コンパイラ制御文の文法

compiler-control-statementconditional-compilation-block
compiler-control-statementline-control-statement
compiler-control-statementdiagnostic-statement



条件コンパイルブロックの文法

conditional-compilation-blockif-directive-clause ­elseif-directive-clauses ­opt else-directive-clause opt ­endif-directive

if-directive-clauseif-directive ­compilation-condition statements ­ opt
elseif-directive-clauseselseif-directive-clause ­elseif-directive-clauses opt­
elseif-directive-clauseelseif-directive­ ­compilation-condition­ statements ­opt­
else-directive-clauseelse-directive statements ­opt
if-directive#if
elseif-directive#elseif
else-directive#else
endif-directive#endif

compilation-conditionplatform-condition
compilation-conditionidentifier
compilation-conditionboolean-literal
compilation-condition( ­compilation-condition )
compilation-condition! ­­compilation-condition
compilation-condition­compilation-condition ­&& ­­compilation-condition
compilation-condition­compilation-condition ­|| ­compilation-condition

platform-conditionos ­( ­operating-system ­)
platform-conditionarch ­( ­architecture ­)
platform-conditionswift ­( ­>= ­swift-version ) | swift ( < ­swift-version )
platform-conditioncompiler ( >= ­swift-version ) | compiler ( < ­swift-version )
platform-conditioncanImport ( module-name )
platform-conditiontargetEnvironment ( environment )

operating-systemmacOS | iOS | watchOS | tvOS
architecturei386 | x86_64 | arm | arm64
swift-versiondecimal-digits swift-version-continuation ­opt
swift-version-continuation → . ­decimal-digits­ swift-version-continuation ­opt
module-nameidentifier
environmentsimulator



行制御文の文法

line-control-statement#sourceLocation ­( ­file: ­file-name ­, ­line: ­line-number ­)
line-control-statement#sourceLocation ­( ­)
line-number → A decimal integer greater than zero
file-namestatic-string-literal­



コンパイル時診断文の文法

diagnostic-statement#error ( diagnostic-message )
diagnostic-statement#warning ( diagnostic-message )

diagnostic-messagestatic-string-literal



利用可能条件の文法

availability-condition#available ­( ­availability-arguments )
availability-argumentsavailability-argument | availability-argument , ­­availability-arguments
availability-argumentplatform-name platform-version­
availability-argument*

platform-nameiOS | iOSApplicationExtension
platform-namemacOS | macOSApplicationExtension
platform-namewatchOS
platform-nametvOS
platform-versiondecimal-digits
platform-versiondecimal-digits ­. ­decimal-digits
platform-versiondecimal-digits ­. decimal-digits ­. decimal-digits



宣言


宣言の文法

declarationimport-declaration
declarationconstant-declaration
declarationvariable-declaration
declarationtypealias-declaration
declarationfunction-declaration
declarationenum-declaration
declarationstruct-declaration
declarationclass-declaration
declarationprotocol-declaration
declarationinitializer-declaration
declarationdeinitializer-declaration
declarationextension-declaration
declarationsubscript-declaration
declarationoperator-declaration
declarationprecedence-group-declaration­
declarationsdeclaration­ declarations ­opt



トップレベルの宣言の文法

top-level-declarationstatements ­opt



コードブロックの文法

code-block{ statements ­opt ­}



import 宣言の文法

import-declarationattributes ­opt ­import ­import-kind ­opt ­import-path

­ import-kindtypealiasstructclass | enumprotocollet | varfunc
import-pathimport-path-identifierimport-path-identifier ­. import-path
import-path-identifieridentifieroperator



定数の宣言の文法

constant-declarationattributes ­opt declaration-modifiers ­opt ­let ­pattern-initializer-list

pattern-initializer-listpattern-initializer | pattern-initializer ­, ­pattern-initializer-list
pattern-initializerpattern initializer ­opt
­ initializer= ­expression



­
変数宣言の文法

variable-declarationvariable-declaration-head ­pattern-initializer-list
variable-declarationvariable-declaration-head ­variable-name ­type-annotation ­code-block
variable-declarationvariable-declaration-head ­­variable-name ­type-annotation getter-setter-block
variable-declarationvariable-declaration-head ­­variable-name ­type-annotation ­getter-setter-keyword-block
variable-declarationvariable-declaration-head ­variable-name ­initializer ­­willSet-didSet-block
variable-declarationvariable-declaration-head ­variable-name ­type-annotation initializer ­opt ­willSet-didSet-block

variable-declaration-headattributes ­opt declaration-modifiers ­opt ­var
variable-nameidentifier

getter-setter-block­code-block
getter-setter-block{ ­getter-clause ­setter-clause ­opt ­}
getter-setter-block{ ­setter-clause ­getter-clause ­}
­ getter-clauseattributes ­opt mutation-modifier ­opt ­get code-block
setter-clauseattributes ­opt mutation-modifier ­­­opt ­set ­setter-name opt ­code-block­
setter-name → ( ­identifier ­)

getter-setter-keyword-block{ ­getter-keyword-clause­ setter-keyword-clause­ ­opt ­}
getter-setter-keyword-block{ ­setter-keyword-clause ­getter-keyword-clause ­}
getter-keyword-clauseattributes opt mutation-modifier ­­­opt ­get
setter-keyword-clauseattributes ­opt mutation-modifier ­­­opt ­set

willSet-didSet-block{ ­willSet-clause ­didSet-clause ­opt ­}
willSet-didSet-block{ ­didSet-clause ­willSet-clause ­opt ­}
willSet-clauseattributes ­opt willSet setter-name ­opt ­code-block­
didSet-clauseattributes ­opt ­didSet ­setter-name ­opt ­code-block



型エイリアス宣言の文法

typealias-declarationattributes opt access-level-modifier opt ­typealias­ typealias-name ­generic-parameter-clause ­opt typealias-assignment
­ typealias-nameidentifier
typealias-assignment= ­type­



関数の宣言の文法

function-declarationfunction-head ­function-name generic-parameter-clause ­opt function-signature generic-where-clause ­opt function-body

function-headattributes ­opt declaration-modifiers ­opt ­func
function-nameidentifieroperator
­
function-signatureparameter-clause throws ­opt ­function-result ­opt
function-signatureparameter-clause rethrows ­function-result ­opt
function-result-> attributes ­opt ­type­
function-bodycode-block­

parameter-clause(­ ) parameter-list ­
parameter-listparameterparameter ­, ­parameter-list
parameter­external-parameter-name ­opt ­local-parameter-name ­type-annotation­ default-argument-clause ­opt
parameter­external-parameter-name ­opt ­local-parameter-name type-annotation­
parameter­external-parameter-name ­opt ­local-parameter-name type-annotation­ . . .
external-parameter-nameidentifier
local-parameter-nameidentifier
­ default-argument-clause → =­ expression­



列挙型の宣言の文法

enum-declarationattributes ­opt access-level-modifier ­opt ­union-style-enum­
enum-declarationattributes opt­ access-level-modifier ­opt ­raw-value-style-enum

union-style-enumindirect opt enum ­enum-name generic-parameter-clause opt type-inheritance-clause opt generic-where-clause ­opt { ­union-style-enum-members­ opt ­}
union-style-enum-membersunion-style-enum-member union-style-enum-members ­opt
union-style-enum-memberdeclarationunion-style-enum-case-clause | compiler-control-statement
union-style-enum-case-clauseattributes ­opt indirect opt ­case ­union-style-enum-case-list
union-style-enum-case-listunion-style-enum-caseunion-style-enum-case ­, ­union-style-enum-case-list
union-style-enum-caseenum-case-name­ tuple-type opt
enum-nameidentifier
enum-case-nameidentifier

raw-value-style-enumenum ­enum-name generic-parameter-clause ­opt type-inheritance-clause ­generic-where-clause opt {­ raw-value-style-enum-members ­}
­ raw-value-style-enum-membersraw-value-style-enum-member ­raw-value-style-enum-members ­opt
raw-value-style-enum-memberdeclaration­ | raw-value-style-enum-case-clause | compiler-control-statement
raw-value-style-enum-case-clauseattributes ­opt ­case ­raw-value-style-enum-case-list
raw-value-style-enum-case-listraw-value-style-enum-case ­| raw-value-style-enum-case ­, ­raw-value-style-enum-case-list­
raw-value-style-enum-caseenum-case-name ­raw-value-assignment ­opt
raw-value-assignment= raw-value-literal­
raw-value-literalnumeric-literalstatic-string-literalboolean-literal


構造体の宣言の文法

struct-declarationattributes ­opt access-level-modifier ­opt ­struct ­struct-name­ generic-parameter-clause ­opt ­type-inheritance-clause ­opt generic-where-clause ­opt ­struct-body
struct-nameidentifier
struct-body struct-members opt ­}

struct-membersstruct-member struct-members ­opt
struct-memberdeclarations­ | compiler-control-statement



クラス宣言の文法

class-declarationattributes opt access-level-modifier ­opt final ­opt ­class ­class-name generic-parameter-clause ­opt ­type-inheritance-clause ­opt generic-where-clause ­opt ­class-body
class-declarationattributes opt final access-level-modifier ­opt ­class ­class-name generic-parameter-clause ­opt ­type-inheritance-clause ­opt generic-where-clause ­opt ­class-body
class-nameidentifier
class-body ­class-members ­opt ­}

class-membersclass-member ­class-members ­opt
­ class-memberdeclarations | compiler-control-statement
­


プロトコル宣言の文法

protocol-declarationattributes ­opt access-level-modifier ­opt ­protocol ­protocol-name­ type-inheritance-clause ­opt generic-where-clause ­opt ­protocol-body
protocol-nameidentifier
protocol-body protocol-members ­opt ­}

protocol-membersprotocol-member protocol-members ­opt
­ protocol-memberprotocol-member-declaration ­| compiler-control-statement

protocol-member-declarationprotocol-property-declaration
protocol-member-declarationprotocol-method-declaration
protocol-member-declarationprotocol-initializer-declaration
protocol-member-declarationprotocol-subscript-declaration
protocol-member-declarationprotocol-associated-type-declaration
protocol-member-declarationstypealias-declaration­



プロトコルプロパティ宣言の文法

protocol-property-declarationvariable-declaration-head ­variable-name ­type-annotation getter-setter-keyword-block



プロトコルメソッド宣言の文法

protocol-method-declarationfunction-head ­function-name generic-parameter-clause ­opt ­function-signature generic-where-clause ­opt



プロトコルイニシャライザ宣言の文法

protocol-initializer-declarationinitializer-head generic-parameter-clause ­opt parameter-clause throws ­opt generic-where-clause ­opt
protocol-initializer-declarationinitializer-head generic-parameter-clause ­opt parameter-clause rethrows generic-where-clause ­opt



プロトコルサブスクリプト宣言の文法

protocol-subscript-declarationsubscript-head­ subscript-result ­generic-where-clause ­opt getter-setter-keyword-block­



プロトコルに関連した型の宣言の文法

protocol-associated-type-declarationattributes ­opt access-level-modifier ­opt ­associatedtype typealias-name ­type-inheritance-clause ­­opt ­­typealias-assignment ­opt generic-where-clause ­opt



イニシャライザ宣言の文法

initializer-declarationinitializer-head generic-parameter-clause ­opt parameter-clause throws ­opt generic-where-clause ­opt initializer-body
initializer-declarationinitializer-head generic-parameter-clause ­opt parameter-clause rethrows generic-where-clause ­opt initializer-body
initializer-headattributes ­opt ­declaration-modifiers ­opt ­init
­ initializer-headattributes opt declaration-modifiers ­opt ­init­ ?
initializer-headattributes ­opt declaration-modifiers ­opt ­init­ !
­ initializer-bodycode-block



デイニシャライザ宣言の文法

deinitializer-declarationattributes ­opt ­deinit ­code-block



機能拡張の宣言の文法

extension-declarationattributes ­opt access-level-modifier ­opt ­extension type-identifier ­type-inheritance-clause ­opt generic-where-clause ­opt ­extension-body
extension-body extension-members ­opt ­}

extension-membersextension-member­ extension-members ­opt­
extension-memberdeclaration | compiler-control-statement
­


サブスクリプト宣言の文法

subscript-declarationsubscript-head subscript-result generic-where-clause ­opt ­code-block
subscript-declarationsubscript-head ­subscript-result generic-where-clause ­opt getter-setter-block
subscript-declarationsubscript-head ­subscript-result generic-where-clause ­opt getter-setter-keyword-block
subscript-headattributes ­opt declaration-modifiers ­opt ­subscript generic-parameter-clause opt parameter-clause
subscript-result-> attributes ­opt ­type



演算子の宣言の文法

operator-declarationprefix-operator-declarationpostfix-operator-declarationinfix-operator-declaration­

prefix-operator-declarationprefix ­operator ­operator
postfix-operator-declarationpostfix ­operator ­operator
infix-operator-declarationinfix­ operator ­operator ­infix-operator-group ­opt

infix-operator-group: precedence-group-name



優先順位グループ宣言の文法

precedence-group-declarationprecedencegroup ­precedence-group-name­ { ­precedence-group-attributes ­opt ­}
precedence-group-attributesprecedence-group-attribute ­precedence-group-attributes ­opt
­ precedence-group-attributeprecedence-group-relation­
precedence-group-attributeprecedence-group-assignment
precedence-group-attributeprecedence-group-associativity­

precedence-group-relationhigherThan ­: ­precedence-group-names­
precedence-group-relationlowerThan ­: ­precedence-group-names­

precedence-group-assignmentassignment ­: ­boolean-literal

precedence-group-associativityassociativity­ : ­left
precedence-group-associativityassociativity ­: ­right
precedence-group-associativityassociativity ­: ­none
precedence-group-namesprecedence-group-name | precedence-group-name ­, ­precedence-group-names
precedence-group-nameidentifier­



宣言修飾子の文法

declaration-modifierclassconvenience ­| dynamic | final | infix | lazy | optional­ | override | postfix | prefix | required | static | unowned | unowned­ ( ­safe ­) | unowned ­( ­unsafe ­)weak
declaration-modifieraccess-level-modifier
declaration-modifiermutation-modifier
declaration-modifiersdeclaration-modifier ­declaration-modifiers ­opt

access-level-modifierprivate | private ­( ­set ­)
access-level-modifierfileprivate­ | fileprivate ­( ­set ­)­
access-level-modifierinternal | internal ­( ­set ­)
access-level-modifierpublic | public ­( ­set ­)
access-level-modifieropen | open ­( ­set ­)

mutation-modifiermutating | nonmutating



属性


属性の文法

attribute@­ attribute-name ­attribute-argument-clause ­opt
­ attribute-nameidentifier­
attribute-argument-clause( balanced-tokens ­opt ­)
attributesattribute attributes ­opt

balanced-tokens­balanced-token balanced-tokens ­opt
balanced-token( balanced-tokens ­opt ­)
balanced-token[ balanced-tokens ­opt ­]
balanced-token balanced-tokens ­opt ­}
balanced-token → Any identifier, keyword, literal, or operator
balanced-token → Any punctuation except (­, )­, [­, ]­, {­, or }



パターン


パターンの文法

patternwildcard-pattern ­­type-annotation­ ­opt
patternidentifier-pattern ­­type-annotation­ ­opt
patternvalue-binding-pattern
patterntuple-pattern ­type-annotation ­opt
patternenum-case-pattern
patternoptional-pattern
­ patterntype-casting-pattern­
patternexpression-pattern



ワイルドカードパターンの文法

wildcard-pattern_



識別子パターンの文法

identifier-patternidentifier



値結合パターンの文法

value-binding-patternvar pattern | let pattern



タプルパターンの文法

tuple-pattern tuple-pattern-element-list ­opt ­)
tuple-pattern-element-listtuple-pattern-element | tuple-pattern-element ­, ­tuple-pattern-element-list­
tuple-pattern-elementpattern | identifier : pattern



列挙型ケースパターンの文法

enum-case-patterntype-identifier ­opt ­. ­enum-case-name ­tuple-pattern ­opt



Optional パターンの文法

optional-patternidentifier-pattern­



型キャストパターンの文法

type-casting-patternis-pattern | as-pattern
is-patternis ­type
as-patternpattern as ­type



式のパターンの文法

expression-patternexpression



汎用パラメータと引数



汎用のパラメータ句の文法

generic-parameter-clause< generic-parameter-list ­>
generic-parameter-listgeneric-parameter | generic-parameter ­, generic-parameter-list
generic-parametertype-name
generic-parametertype-name : type-identifier
­ generic-parametertype-name protocol-composition-type

generic-where-clausewhere requirement-list­
requirement-listrequirement | requirement ­, ­requirement-list­
requirementconformance-requirement | same-type-requirement

conformance-requirementtype-identifier ­: ­type-identifier
conformance-requirementtype-identifier ­: protocol-composition-type
same-type-requirementtype-identifier ­== ­type



­
汎用引数句の文法

generic-argument-clause< ­generic-argument-list­ >
generic-argument-listgeneric-argument | generic-argument ­, ­generic-argument-list
generic-argumenttype


前:汎用パラメータと引数 後:マニュアルの変更履歴

<BETA SOFTWARE>
このドキュメントには、開発中の API または技術に関する予備的な情報が含まれています。この情報は変更されることがあり、このドキュメントに従って実装されたソフトウェアは、最終的なオペレーティングシステムソフトウェアでテストする必要があります。

Apple の Beta ソフトウエアについての詳細


















トップへ












トップへ












トップへ












トップへ
目次
Xcode 10 の新機能

Swift について
Swift と Cocoa と Objective-C (obsolete)
Swift Blog より (obsolete)

SwiftLogo
  • Swift 5.0 全メニュー


  • Swift へようこそ
  • Swift について

  • Swift 言語のガイド

  • 言語リファレンス
  • 言語リファレンスについて
  • 語彙の構造



  • 宣言
  • 属性
  • パターン
  • 汎用パラメータと引数
  • 文法のまとめ

  • マニュアルの変更履歴













  • トップへ












    トップへ












    トップへ












    トップへ












    トップへ












    トップへ












    トップへ












    トップへ












    トップへ












    トップへ