//
で始まり、ラインフィード(U+000A) またはキャリッジリターン(U+000D) まで続きます。複数行のコメントは /*
で始まり、*/
で終わります。複数行のコメントをネストすることはできますが、コメントマーカーで始まりと終わりを囲む必要があります。GRAMMAR OF WHITESPACE whitespace → whitespace-item whitespaceopt whitespace-item → line-break whitespace-item → inline-space whitespace-item → comment whitespace-item → multiline-comment whitespace-item → U+0000, U+000B, または U+000C line-break → U+000A line-break → U+000D line-break → U+000D に続く U+000A inline-spaces → inline-space inline-spacesopt inline-space → U+0009 または U+0020 comment →//
comment-text line-break multiline-comment →/*
multiline-comment-text*/
comment-text → comment-text-item comment-textopt comment-text-item → U+000A または U+000D を除く任意の Unicode スカラ値 multiline-comment-text → multiline-comment-text-item multiline-comment-textopt multiline-comment-text-item → multiline-comment multiline-comment-text-item → comment-text-item multiline-comment-text-item →/*
または*/
を除く任意の Unicode スカラ値
A
から Z
、アンダースコア(_
)、基本多言語平面内の非結合英数 Unicode
文字、または私用面にない基本多言語平面外の文字で始まります。最初の文字の後ろに数字と Unicode
文字を組み合わせることもできます。public
アクセスレベル修飾子がある場合でも、アンダースコアで始まる識別子を internal として扱います。この規則により、宣言を public にしなければならない制限がある場合でも、フレームワークの作成者はクライアントが対話したり依存したりしてはならない API の一部をマークできます。さらに、2 つのアンダースコアで始まる識別子は Swift コンパイラと標準ライブラリ用に予約されています。class
は有効な識別子ではありませんが、`class` は有効です。バッククォートは識別子の一部とは見なされません。`x`と x
は同じ意味です。$0
、$1
、$2
などの名前が付けられます。これらの名前は、クロージャのスコープ内で有効な識別子です。$
)で始まる識別子を合成します。コード上でこれらの識別子とやり取りできますが、ドル記号($
)から始まる識別子を宣言することはできません。詳細については、Attributes(属性)の章のpropertyWrapperセクションを参照ください。GRAMMAR OF AN IDENTIFIER identifier → identifier-head identifier-charactersopt identifier → ` identifier-head identifier-charactersopt ` identifier → implicit-parameter-name identifier → property-wrapper-projection identifier-list → identifier | identifier,
identifier-list identifier-head → 大文字または小文字の A ~ Z identifier-head →_
identifier-head → U+00A8, U+00AA, U+00AD, U+00AF, U+00B2–U+00B5, または U+00B7–U+00BA identifier-head → U+00BC–U+00BE, U+00C0–U+00D6, U+00D8–U+00F6, または U+00F8–U+00FF identifier-head → U+0100–U+02FF, U+0370–U+167F, U+1681–U+180D, または 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, または U+2060–U+206F identifier-head → U+2070–U+20CF, U+2100–U+218F, U+2460–U+24FF, または U+2776–U+2793 identifier-head → U+2C00–U+2DFF または U+2E80–U+2FFF identifier-head → U+3004–U+3007, U+3021–U+302F, U+3031–U+303F, または U+3040–U+D7FF identifier-head → U+F900–U+FD3D, U+FD40–U+FDCF, U+FDF0–U+FE1F, または U+FE30–U+FE44 identifier-head → U+FE47–U+FFFD identifier-head → U+10000–U+1FFFD, U+20000–U+2FFFD, U+30000–U+3FFFD, または U+40000–U+4FFFD identifier-head → U+50000–U+5FFFD, U+60000–U+6FFFD, U+70000–U+7FFFD, または U+80000–U+8FFFD identifier-head → U+90000–U+9FFFD, U+A0000–U+AFFFD, U+B0000–U+BFFFD, または U+C0000–U+CFFFD identifier-head → U+D0000–U+DFFFD または U+E0000–U+EFFFD identifier-character → 数値 0 ~ 9 identifier-character → U+0300–U+036F, U+1DC0–U+1DFF, U+20D0–U+20FF, または U+FE20–U+FE2F identifier-character → identifier-head identifier-characters → identifier-character identifier-charactersopt implicit-parameter-name →$
decimal-digits property-wrapper-projection →$
identifier-characters
inout
、var
、let
以外のキーワードは、バッククォートでエスケープすることなく関数宣言、または関数の引数名として使用できます。メンバの名前がキーワードと同じ場合、およびメンバの参照とキーワードの使用の間に曖昧さがある場合を除いて、そのメンバへの参照をバッククォートでエスケープする必要はありません。しかし、例えば self
、Type
、および Protocol
には明示的メンバ式の中で特別な意味があるため、そのコンテキストではバッククォートによるエスケープが必要です。associatedtype
、class
、deinit
、enum
、extension
、fileprivate
、func
、import
、init
、inout
、internal
、let
、open
、operator
、private
、protocol
、public
、rethrows
、static
、struct
、subscript
、typealias
、および var
break
、case
、continue
、default
、defer
、do
、else
、fallthrough
、for
、guard
、if
、in
、repeat
、return
、switch
、where
、while
as
、Any
、catch
、false
、is
、nil
、super
、self
、Self
、throw
、throws
、true
、および try
_
#
)で始まるキーワード: #available
、#colorLiteral
、#column
、#else
、#elseif
、#endif
、#error
、#file
、#fileID
、#fileLiteral
、#filePath
、#function
、#if
、#imageLiteral
、#line
、#selector
、#sourceLocation
、および #warning
associativity
、convenient
、dynamic
、didSet
、final
、get
、infix
、indirect
、lazy
、left
、mutating
、none
、nonmutating
、optional
、override
、postfix
、precedence
、prefix
、プロトコル、Protocol
、 required
、right
、set
、Type
、 unowned
、 weak
、 および willSet
。該当するコンテキスト外では、識別子として使用することができます(
、 )
、 {
、 }
、 [
、 ]
、 .
、 ,
、 :
、 ;
、 =
、 @
、 #
、 &
(前置演算子として)、->
、`、?
、および !
(後置演算子として)let x: Int8 = 42
では、Swift は明示的な型注釈(: Int8
)を使用して整数リテラル 42
の型が Int8
だと推論します。適切な型情報が利用できない場合、Swift はリテラルの型が Swift 標準ライブラリで定義されているデフォルトのリテラル型の 1 つだと推論します。デフォルトの型は、整数リテラルの場合は Int
、浮動小数点リテラルの場合は Double
、文字列リテラルの場合は String
、ブールリテラルの場合は Bool
です。例えば、let str = "Hello, world"
という宣言では、文字列リテラル "Hello, world"
のデフォルトの推論型は String
です。ExpressibleByIntegerLiteral
、浮動小数点リテラルは ExpressibleByFloatLiteral
、文字列リテラルは ExpressibleByStringLiteral
、ブールリテラルは ExpressibleByBooleanLiteral
、単一の Unicode スカラのみを含む文字列リテラルは ExpressibleByUnicodeScalarLiteral
、および単一の拡張書記素クラスタのみを含む文字列リテラルは ExpressibleByExtendedGraphemeClusterLiteral
。例えば、Int8
は ExpressibleByIntegerLiteral
プロトコルに準拠しているため、宣言 let x: Int8 = 42
の整数リテラル 42
の型注釈で使用できます。GRAMMAR OF A LITERAL 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
0b
で始まり、8 進数リテラルは 0o
で始まり、16 進数リテラルは 0x
で始まります。0
~ 9
の数字が含まれます。2 進数リテラルには 0
と 1
が含まれ、8 進数リテラルには 0
~ 7
が含まれ、16 進数リテラルには 0
~ 9
および大文字または小文字の A
~ F
が含まれます。-42
のように、整数リテラルの前にマイナス記号(-
)を付けることで表現されます。_
)を使用できますが、それ自体は無視されるのでリテラルの値には影響しません。整数リテラルの先頭はゼロ(0
)で始めることができますが、同様に無視されるのでリテラルの基数または値には影響しません。Int
型です。Swift 標準ライブラリは、Integers(整数)で説明されているように、様々なサイズの符号付き整数および符号なし整数の型も定義します。GRAMMAR OF AN INTEGER LITERAL integer-literal → binary-literal integer-literal → octal-literal integer-literal → decimal-literal integer-literal → hexadecimal-literal binary-literal →0b
binary-digit binary-literal-charactersopt binary-digit → 数値 0 または 1 binary-literal-character → binary-digit |_
binary-literal-characters → binary-literal-character binary-literal-charactersopt octal-literal →0o
octal-digit octal-literal-charactersopt octal-digit → 数値 0 ~ 7 octal-literal-character → octal-digit |_
octal-literal-characters → octal-literal-character octal-literal-charactersopt decimal-literal → decimal-digit decimal-literal-charactersopt decimal-digit → 数値 0 ~ 9 decimal-digits → decimal-digit decimal-digitsopt decimal-literal-character → decimal-digit |_
decimal-literal-characters → decimal-literal-character decimal-literal-charactersopt hexadecimal-literal →0x
hexadecimal-digit hexadecimal-literal-charactersopt hexadecimal-digit → 数値 0 ~ 9、 a ~ f、または A ~ F hexadecimal-literal-character → hexadecimal-digit |_
hexadecimal-literal-characters → hexadecimal-literal-character hexadecimal-literal-charactersopt
0x
前置付き)で表すこともできます。.
)とそれに続く一連の 10 進数で構成されます。指数は大文字または小文字の前置の e
とその後に続く 10 進数の数字のシーケンスで構成され、e
の前の値に 10 の累乗が掛けられます。例えば 1.25e2
は 1.25 x 125.0
と評価されます。同様に、1.25e-2
は 1.25 x 0.0125
と評価されます。0x
、オプションの 16 進数の小数、16 進数の指数で構成されます。16 進数は小数点とそれに続く 16 進数の数字で構成されます。指数は大文字または小文字の前置の p
とその後に続く 10 進数の数字のシーケンスで構成されます。これは p
の前の値に乗算される 2 のべき乗を示します。例えば 0xFp2
は 15 x 60
と評価されます。同様に 0xFp-2
は 15 x 3.75
と評価されます。-42.5
のようにリテラルの前にマイナス記号(-
)を付けることで表現されます。_
)を使用できますが、無視されるため、リテラルの値には影響しません。浮動小数点リテラルの先行はゼロ(0
)で始めることができますが、同様に無視されるのでリテラルの基数または値には影響しません。Double
型です。Swift 標準ライブラリでは 32 ビットの浮動小数点数を表す Float
型も定義されています。GRAMMAR OF A FLOATING-POINT LITERAL floating-point-literal → decimal-literal decimal-fractionopt decimal-exponentopt floating-point-literal → hexadecimal-literal hexadecimal-fractionopt hexadecimal-exponent decimal-fraction →.
decimal-literal decimal-exponent → floating-point-e signopt decimal-literal hexadecimal-fraction →.
hexadecimal-digit hexadecimal-literal-charactersopt hexadecimal-exponent → floating-point-p signopt decimal-literal floating-point-e →e
|E
floating-point-p →p
|P
sign →+
|-
"
)、エスケープされていないバックスラッシュ (\
)、キャリッジリターン、またはラインフィードを含めることはできません。"
)、キャリッジリターン、およびラインフィードを含めることができます。3 つのエスケープされていない二重引用符を隣り合わせに含めることはできません。"""
の後の改行は文字列の一部ではありません。リテラルを終了する """
の前の改行も文字列の一部ではありません。改行で開始または終了する複数行の文字列リテラルを作成するには、最初または最後の行として空白行を挿入します。"""
がインデントを決定します。リテラル内のすべての非空白行は、終わりの """
の前に表示されるものと同じインデントで開始する必要があります。タブとスペース間の変換はありません。インデントの後に追加のスペースとタブを含めることができます。これらのスペースとタブは文字列に表示されます。\
)を記述すると文字列からその改行が省略されます。バックスラッシュと改行の間の空白もすべて省略されます。この構文を使用すると、結果の文字列の値を変更せずにソースコードで複数行の文字列リテラルをハードラップ(文字数を指定して自動改行)することができます。\0
)\
)\"
)\'
)\u{n}
)、n
は 1 ~ 8 桁の 16 進数\
)の後に括弧で囲みます。補間された式には文字列リテラルを含めることができますが、エスケープされていないバックスラッシュ、キャリッジリターン、またはラインフィードを含めることはできません。#
) のセットです。拡張区切り文字で区切られた文字列には次の形式があります:+
演算子によって連結された文字列リテラルはコンパイル時に連結されます。例えば下記の例の textA
と textB
の値は同じで、実行時に連結は実行されません。GRAMMAR OF A STRING LITERAL string-literal → static-string-literal | interpolated-string-literal string-literal-opening-delimiter → extended-string-literal-delimiteropt"
string-literal-closing-delimiter →"
extended-string-literal-delimiteropt static-string-literal → string-literal-opening-delimiter quoted-textopt string-literal-closing-delimiter static-string-literal → multiline-string-literal-opening-delimiter multiline-quoted-textopt multiline-string-literal-closing-delimiter multiline-string-literal-opening-delimiter → extended-string-literal-delimiter"""
multiline-string-literal-closing-delimiter →"""
extended-string-literal-delimiter extended-string-literal-delimiter →#
extended-string-literal-delimiteropt quoted-text → quoted-text-item quoted-textopt quoted-text-item → escaped-character quoted-text-item → "、\、 U+000A、または U+000D を除く任意のUnicodeスカラ値 multiline-quoted-text → multiline-quoted-text-item multiline-quoted-textopt multiline-quoted-text-item → escaped-character multiline-quoted-text-item → \ を除く任意のUnicodeスカラ値 multiline-quoted-text-item → escaped-newline interpolated-string-literal → string-literal-opening-delimiter interpolated-textopt string-literal-closing-delimiter interpolated-string-literal → multiline-string-literal-opening-delimiter multiline-interpolated-textopt multiline-string-literal-closing-delimiter interpolated-text → interpolated-text-item interpolated-textopt interpolated-text-item →\(
expression)
| quoted-text-item multiline-interpolated-text → multiline-interpolated-text-item multiline-interpolated-textopt multiline-interpolated-text-item →\(
expression)
| multiline-quoted-text-item escape-sequence →\
extended-string-literal-delimiter escaped-character → escape-sequence0
| escape-sequence\
| escape-sequencet
| escape-sequencen
| escape-sequencer
| escape-sequence"
| escape-sequence'
escaped-character → escape-sequenceu
{
unicode-scalar-digits}
unicode-scalar-digits → 0 から 8 までの16進数 escaped-newline → escape-sequence inline-spacesopt line-break
GRAMMAR OF A REGULAR EXPRESSION LITERAL regular-expression-literal → regular-expression-literal-opening-delimiter regular-expression regular-expression-literal-closing-delimiter regular-expression → Any regular expression regular-expression-literal-opening-delimiter → extended-regular-expression-literal-delimiter opt / regular-expression-literal-closing-delimiter → / extended-regular-expression-literal-delimiter opt extended-regular-expression-literal-delimiter → # extended-regular-expression-literal-delimiter opt
/
、 =
、 -
、+
、!
、*
、%
、<
、>
、&
、|
、^
、?
、または 〜
または下記の文法で定義されている Unicode 文字の 1 つ(これは、数学演算子、その他のシンボル、および装飾記号 Unicode ブロックからの文字を含む)で始めることができます。最初の文字の後に Unicode 文字を組み合わせることもできます。.
)で始まるカスタム演算子を定義することもできます。これらの演算子は追加のドットを含めることができます。例えば .+.
は単一の演算子として扱われます。演算子がドットで始まらない場合は他の場所にドットを含めることはできません。例えば +.+
は +
演算子の後に .+
演算子が続いているものとして扱われます。?
)を含むカスタム演算子を定義することはできますが、単一の疑問符文字のみで構成することはできません。さらに、感嘆符(!
)を含めることができますが、後置演算子は疑問符または感嘆符で始めることはできません。NOTE トークン=
、-
>
、//
、/ *
、* /
、.
、前置演算子<
、&
、および?
、中置演算子?
、および 後置演算子>
、!
は既に予約されています。これらのトークンはオーバーロードすることも、カスタム演算子として使用することもできません。
a+++b
と a +++ b
にある +++
演算子は二項演算子として扱われますa +++b
にある +++
演算子は、前置単項演算子として扱われますa+++ b
の +++
演算子は後置単項演算子として扱われます.
)が続いている場合は、後置単項演算子として扱われます。例として、a+++.b
にある +++
演算子は後置単項演算子(a +++ .b
ではなく a+++ .b
)として扱われます(
、[
、{
、)
、演算子の後の )
、]
、}
、および ,
、;
、:
も空白と見なされます。!
または ?
のような定義済み演算子は、左側に空白がなければ後置演算子として扱われます。これは右側に空白があるかどうかを気にしません。?
オプショナルチェーン演算子として使用する場合には、左側に空白を入れないでください。三項演算子(?:
)として使用する場合には、両側に空白を入れなければなりません。<
または >
を持つ演算子が 2 つ以上のトークンに分割されることもあります。残りの部分も同じ方法で扱われ、再び分割される可能性があります。結果、Dictionary<String、Array<Int>>
のようなコンストラクト内において >
の間に曖昧さを避ける目的で空白を追加する必要はありません。この例にある >
は誤って単一のトークンのビットシフト(>>
)としては解釈されません。GRAMMAR OF OPERATORS operator → operator-head operator-charactersopt operator → dot-operator-head dot-operator-characters operator-head →/
|=
|-
|+
|!
|*
|%
|<
|>
|&
||
|^
|~
|?
operator-head → U+00A1–U+00A7 operator-head → U+00A9 または U+00AB operator-head → U+00AC または U+00AE operator-head → U+00B0–U+00B1 operator-head → U+00B6, U+00BB, U+00BF, U+00D7, または 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-character → operator-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-characters → operator-character operator-charactersopt dot-operator-head →.
dot-operator-character →.
| operator-character dot-operator-characters → dot-operator-character dot-operator-charactersopt binary-operator → operator prefix-operator → operator postfix-operator → operator