Lexical Structure 語彙の構造

The lexical structure of Swift describes what sequence of characters form valid tokens of the language. These valid tokens form the lowest-level building blocks of the language and are used to describe the rest of the language in subsequent chapters. A token consists of an identifier, keyword, punctuation, literal, or operator. スウィフトの語彙の構造は、どんな文字の連なりがこの言語の公式のトークン(個々の語、最少単位の語)を作り上げるかについて述べます。これらの公式のトークンは、言語の最も基本的な建物ブロックを形成して、以降の章において言語の残りの部分を記述するために使われます。あるトークンは1つの識別子、キーワード、句読点、リテラル、または演算子からなります。

In most cases, tokens are generated from the characters of a Swift source file by considering the longest possible substring from the input text, within the constraints of the grammar that are specified below. This behavior is referred to as longest match or maximal munch. ほとんどの場合、トークンは、スウィフトのソースファイルの文字から、下で指定される文法の制約の範囲内で、その入力テキストからの最も長く取ることのできる下位文字列を考慮することによって生成されます。この挙動は、最長マッチまたは欲張り捕獲と呼ばれます。

Whitespace and Comments 空白とコメント

Whitespace has two uses: to separate tokens in the source file and to distinguish between prefix, postfix, and infix operators (see Operators), but is otherwise ignored. The following characters are considered whitespace: space (U+0020), line feed (U+000A), carriage return (U+000D), horizontal tab (U+0009), vertical tab (U+000B), form feed (U+000C) and null (U+0000). 空白には、2つの用途があります:ソース・ファイルの中のトークンを切り離すため、そして接頭辞、接尾辞、および接中辞演算子の間の区別をするため(演算子を見てください)、しかしそれ以外では無視されます。以下の文字は、空白とみなされます:空白(U+0020)、行送り(U+000A)、復帰(U+000D)、水平タブ(U+0009)、垂直タブ(U+000B)、改ページ(U+000C)およびヌル(U+0000)。

Comments are treated as whitespace by the compiler. Single line comments begin with // and continue until a line feed (U+000A) or carriage return (U+000D). Multiline comments begin with /* and end with */. Nesting multiline comments is allowed, but the comment markers must be balanced. コメントは、コンパイラによって空白とみなされます。一行コメントは//で始まります、そして行送り(U+000A)または復帰(U+000D)まで続きます。複数行コメントは/*で始まり*/で終わります。複数行コメントが入れ子にされることは許されます、しかしコメント目印が釣り合っている必要があります。

Comments can contain additional formatting and markup, as described in Markup Formatting Reference. コメントは、さらに追加の書式とマークアップを含む事が、Markup Formatting Referenceで記述されるように可能です。

Grammar of whitespace 空白の文法

whitespace whitespace-item whitespace opt

whitespace-item line-break

whitespace-item inline-space

whitespace-item comment

whitespace-item multiline-comment

whitespace-item U+0000, U+000B, or U+000C

line-break U+000A

line-break U+000D

line-breakU+000D followed by U+000A U+000D に続けて U+000A

inline-spaces inline-space inline-spaces opt

inline-space U+0009 or U+0020

comment // comment-text line-break

multiline-comment /* multiline-comment-text */

comment-text comment-text-item comment-text opt

comment-text-itemAny Unicode scalar value except U+000A or U+000D 任意のユニコードスカラー値、 しかし U+000A または U+000D を除く

multiline-comment-text multiline-comment-text-item multiline-comment-text opt

multiline-comment-text-item multiline-comment

multiline-comment-text-item comment-text-item

multiline-comment-text-itemAny Unicode scalar value except /* or */ 任意のユニコードスカラー値、 しかし/* または */ を除く

Identifiers 識別子

Identifiers begin with an uppercase or lowercase letter A through Z, an underscore (_), a noncombining alphanumeric Unicode character in the Basic Multilingual Plane, or a character outside the Basic Multilingual Plane that isn’t in a Private Use Area. After the first character, digits and combining Unicode characters are also allowed. 識別子は、大文字または小文字のAからZ、アンダースコア(_)、基本多言語面の非合成英数字のUnicode文字、または基本他言語面の外側ての使用領域の中でない文字で始まります。最初の文字の後は、桁および合成Unicode文字もまた、許されます。

Treat identifiers that begin with an underscore as internal, even if their declaration has the public access-level modifier. This convention lets framework authors mark part of an API that clients must not interact with or depend on, even though some limitation requires the declaration to be public. In addition, identifiers that begin with two underscores are reserved for the Swift compiler and standard library. あるアンダースコアで始まる識別子を内部として扱ってください、たとえそれらの宣言がpublicアクセス水準修飾子を持つとしてもです。この慣例は、フレームワーク作者にあるAPIの一部を印させます、それはクライアントがそれと相互作用すべきでないまたはそれに依存すべきでないものです、たとえ何らかの限界がその宣言にパブリックであることを要求しても。さらに、2つのアンダースコアで始まる識別子は、スウィフトコンバイラおよび標準ライブラリのために予約されます。

To use a reserved word as an identifier, put a backtick (`) before and after it. For example, class isn’t a valid identifier, but `class` is valid. The backticks aren’t considered part of the identifier; `x` and x have the same meaning. 識別子として予約語を使用するために、それの前後にバッククォート(`)を置いてください。例えば、classは有効な識別子ではありません、しかし、`class`は有効です。バッククォートは、識別子の一部とみなされません;`x`xは、同じ意味を持ちます。

Inside a closure with no explicit parameter names, the parameters are implicitly named $0, $1, $2, and so on. These names are valid identifiers within the scope of the closure. 明確なパラメータ名のないクロージャの内部で、パラメータは暗黙のうちに$0$1$2、などと名前をつけられます。これらの名前は、クロージャのスコープ内で有効な識別子です。

The compiler synthesizes identifiers that begin with a dollar sign ($) for properties that have a property wrapper projection. Your code can interact with these identifiers, but you can’t declare identifiers with that prefix. For more information, see the propertyWrapper section of the Attributes chapter. コンパイラは、ドル記号($)で始まる識別子を、プロパティラッパー投影を持つプロパティに対して合成します。あなたのコードは、それらの識別子と相互作用できます、しかしあなたは識別子をその接頭辞で宣言できません。さらなる情報として、属性の章のpropertyWrapperの節を見てください。

Grammar of an identifier 識別子の文法

identifier identifier-head identifier-characters opt

identifier ` identifier-head identifier-characters opt `

identifier implicit-parameter-name

identifier property-wrapper-projection

identifier-list identifier | identifier , identifier-list

identifier-headUpper- or lowercase letter A through Z 大文字または小文字のAからZ

identifier-head _

identifier-headU+00A8, U+00AA, U+00AD, U+00AF, U+00B2–U+00B5, or U+00B7–U+00BA U+00A8, U+00AA, U+00AD, U+00AF, U+00B2–U+00B5, または U+00B7–U+00BA

identifier-headU+00BC–U+00BE, U+00C0–U+00D6, U+00D8–U+00F6, or U+00F8–U+00FF U+00BC–U+00BE, U+00C0–U+00D6, U+00D8–U+00F6, または U+00F8–U+00FF

identifier-headU+0100–U+02FF, U+0370–U+167F, U+1681–U+180D, or U+180F–U+1DBF U+0100–U+02FF, U+0370–U+167F, U+1681–U+180D, または U+180F–U+1DBF

identifier-head U+1E00–U+1FFF

identifier-headU+200B–U+200D, U+202A–U+202E, U+203F–U+2040, U+2054, or U+2060–U+206F U+200B–U+200D, U+202A–U+202E, U+203F–U+2040, U+2054, または U+2060–U+206F

identifier-headU+2070–U+20CF, U+2100–U+218F, U+2460–U+24FF, or U+2776–U+2793 U+2070–U+20CF, U+2100–U+218F, U+2460–U+24FF, または U+2776–U+2793

identifier-head U+2C00–U+2DFF or U+2E80–U+2FFF

identifier-headU+3004–U+3007, U+3021–U+302F, U+3031–U+303F, or U+3040–U+D7FF U+3004–U+3007, U+3021–U+302F, U+3031–U+303F, または U+3040–U+D7FF

identifier-headU+F900–U+FD3D, U+FD40–U+FDCF, U+FDF0–U+FE1F, or U+FE30–U+FE44 U+F900–U+FD3D, U+FD40–U+FDCF, U+FDF0–U+FE1F, または U+FE30–U+FE44

identifier-head U+FE47–U+FFFD

identifier-headU+10000–U+1FFFD, U+20000–U+2FFFD, U+30000–U+3FFFD, or U+40000–U+4FFFD U+10000–U+1FFFD, U+20000–U+2FFFD, U+30000–U+3FFFD, または U+40000–U+4FFFD

identifier-headU+50000–U+5FFFD, U+60000–U+6FFFD, U+70000–U+7FFFD, or U+80000–U+8FFFD U+50000–U+5FFFD, U+60000–U+6FFFD, U+70000–U+7FFFD, または U+80000–U+8FFFD

identifier-headU+90000–U+9FFFD, U+A0000–U+AFFFD, U+B0000–U+BFFFD, or U+C0000–U+CFFFD U+90000–U+9FFFD, U+A0000–U+AFFFD, U+B0000–U+BFFFD, または U+C0000–U+CFFFD

identifier-headU+D0000–U+DFFFD or U+E0000–U+EFFFD U+D0000–U+DFFFD または U+E0000–U+EFFFD

identifier-characterDigit 0 through 9 アラビア数字の0から9

identifier-characterU+0300–U+036F, U+1DC0–U+1DFF, U+20D0–U+20FF, or U+FE20–U+FE2F 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-characters opt

implicit-parameter-name $ decimal-digits

property-wrapper-projection $ identifier-characters

Keywords and Punctuation キーワードと句読点

The following keywords are reserved and can’t be used as identifiers, unless they’re escaped with backticks, as described above in Identifiers. Keywords other than inout, var, and let can be used as parameter names in a function declaration or function call without being escaped with backticks. When a member has the same name as a keyword, references to that member don’t need to be escaped with backticks, except when there’s ambiguity between referring to the member and using the keyword—for example, self, Type, and Protocol have special meaning in an explicit member expression, so they must be escaped with backticks in that context. 上の識別子で記述されるように、以下のキーワードは予約済で、それらがバッククォートでエスケープされない限り、識別子として使われることはできません。inoutvar、そしてlet以外のキーワードは、パラメータ名として関数宣言または関数呼び出しにおいてバッククォートでエスケープされることなく使われることができます。あるメンバーがあるキーワードと同じ名前を持つ時、そのメンバーへの参照はバッククォートでエスケープされる必要はありません、そのメンバーへの参照とキーワードの使用の間に曖昧さがある場合を除いては—例えば、selfType、そしてProtocolは、特別な意味をある明示的メンバー式の中で持ちます、それでそれらはバッククォートでエスケープされることがその文脈においては必要です。

  • Keywords used in declarations: associatedtype, class, deinit, enum, extension, fileprivate, func, import, init, inout, internal, let, open, operator, private, precedencegroup, protocol, public, rethrows, static, struct, subscript, typealias, and var. 宣言において使用されるキーワード:associatedtype, class, deinit, enum, extension, fileprivate, func, import, init, inout, internal, let, open, operator, private, precedencegroup, protocol, public, rethrows, static, struct, subscript, typealias, そして var
  • Keywords used in statements: break, case, catch, continue, default, defer, do, else, fallthrough, for, guard, if, in, repeat, return, throw, switch, where, and while. 文において使用されるキーワード:break, case, catch, continue, default, defer, do, else, fallthrough, for, guard, if, in, repeat, return, throw, switch, where, そして while
  • Keywords used in expressions and types: Any, as, catch, false, is, nil, rethrows, self, Self, super, throw, throws, true, and try. 式と型において使用されるキーワード:Any, as, catch, false, is, nil, rethrows, self, Self, super, throw, throws, true, そして try
  • Keywords used in patterns: _. パターンにおいて使用されるキーワード:_
  • Keywords that begin with a number sign (#): #available, #colorLiteral, #column, #dsohandle, #elseif, #else, #endif, #error, #fileID, #fileLiteral, #filePath, #file, #function, #if, #imageLiteral, #keyPath, #line, #selector, #sourceLocation, and #warning. シャープ記号(#)で始まるキーワード:#available, #colorLiteral, #column, #dsohandle, #elseif, #else, #endif, #error, #fileID, #fileLiteral, #filePath, #file, #function, #if, #imageLiteral, #keyPath, #line, #selector, #sourceLocation, そして #warning
  • Keywords reserved in particular contexts: associativity, convenience, didSet, dynamic, final, get, indirect, infix, lazy, left, mutating, none, nonmutating, optional, override, postfix, precedence, prefix, Protocol, required, right, set, some, Type, unowned, weak, and willSet. Outside the context in which they appear in the grammar, they can be used as identifiers. 特定の前後関係において予約されるキーワード:associativity, convenience, didSet, dynamic, final, get, indirect, infix, lazy, left, mutating, none, nonmutating, optional, override, postfix, precedence, prefix, Protocol, required, right, set, some, Type, unowned, weak, そして willSet。そこにおいてそれらが文法の中に現れる文脈外では、それらは識別子として使われることができます。

The following tokens are reserved as punctuation and can’t be used as custom operators: (, ), {, }, [, ], ., ,, :, ;, =, @, #, & (as a prefix operator), ->, `, ?, and ! (as a postfix operator). 以下のトークンは、句読点として予約されます、そしてあつらえの演算子として使われることができません:(, ), {, }, [, ], ., ,, :, ;, =, @, #, &(接頭辞演算子として), ->, `, ?, そして!(接尾辞演算子として)。

Literals 様々なリテラル

A literal is the source code representation of a value of a type, such as a number or string. リテラルは、ある型のある値をソースコードに表わしたものです、例えばある数や文字列など。

The following are examples of literals: 以下はリテラルの例です:

  1. 42 // Integer literal(リテラル整数)
  2. 3.14159 // Floating-point literal(リテラル浮動小数点)
  3. "Hello, world!" // String literal(リテラル文字列)
  4. true // Boolean literal(ブールのリテラル)

A literal doesn’t have a type on its own. Instead, a literal is parsed as having infinite precision and Swift’s type inference attempts to infer a type for the literal. For example, in the declaration let x: Int8 = 42, Swift uses the explicit type annotation (: Int8) to infer that the type of the integer literal 42 is Int8. If there isn’t suitable type information available, Swift infers that the literal’s type is one of the default literal types defined in the Swift standard library. The default types are Int for integer literals, Double for floating-point literals, String for string literals, and Bool for Boolean literals. For example, in the declaration let str = "Hello, world", the default inferred type of the string literal "Hello, world" is String. リテラルはそれ独自の型を持ちません。そうではなく、リテラルは無限精度を持つものとして解析されて、スウィフトの型推論はそのリテラルの型を推測することを試みます。例えば、宣言let x: Int8 = 42において、スウィフトは明示的な型注釈(: Int8)を使って、整数リテラル42の型はInt8であると推論します。適した型情報が得られなかったならば、スウィフトはそのリテラルの型はスウィフト標準ライブラリで定義される省略時のリテラル型の1つと推論します。省略時の型は、整数リテラルのためのInt、浮動小数点リテラルのためのDouble、文字列リテラルのためのString、そしてブールのリテラルのためのBoolです。例えば、宣言let str = "Hello, world"において、文字列リテラル"Hello, world"の省略時の推論される型はStringです。

When specifying the type annotation for a literal value, the annotation’s type must be a type that can be instantiated from that literal value. That is, the type must conform to one of the following Swift standard library protocols: ExpressibleByIntegerLiteral for integer literals, ExpressibleByFloatLiteral for floating-point literals, ExpressibleByStringLiteral for string literals, ExpressibleByBooleanLiteral for Boolean literals, ExpressibleByUnicodeScalarLiteral for string literals that contain only a single Unicode scalar, and ExpressibleByExtendedGraphemeClusterLiteral for string literals that contain only a single extended grapheme cluster. For example, Int8 conforms to the ExpressibleByIntegerLiteral protocol, and therefore it can be used in the type annotation for the integer literal 42 in the declaration let x: Int8 = 42. あるリテラル値に型注釈を指定するとき、その注釈の型は、そのリテラル値からインスタンス化されることができる型でなければなりません。すなわち、その型は以下のスウィフト標準ライブラリのプロトコルの1つに準拠しなければなりません:整数リテラルに対してExpressibleByIntegerLiteral、浮動小数点リテラルに対してExpressibleByFloatLiteral、文字列リテラルに対してExpressibleByStringLiteral、ブールのリテラルに対してExpressibleByBooleanLiteral、ただ1つのユニコードスカラーだけを含む文字列リテラルに対してExpressibleByUnicodeScalarLiteral、そしてただ1つの拡張書記素クラスタだけを含む文字列リテラルに対してExpressibleByExtendedGraphemeClusterLiteral。例えば、Int8ExpressibleByIntegerLiteralプロトコルに準拠します、したがってそれは宣言let x: Int8 = 42の中の整数リテラル42のための型注釈において使われることができます。

Grammar of a literal リテラルの文法

numeric-literal -opt integer-literal | -opt floating-point-literal

boolean-literal true | false

nil-literal nil

Integer Literals 整数リテラル

Integer literals represent integer values of unspecified precision. By default, integer literals are expressed in decimal; you can specify an alternate base using a prefix. Binary literals begin with 0b, octal literals begin with 0o, and hexadecimal literals begin with 0x. 整数リテラルは、無限精度の整数値を表します。初期状態では、整数リテラルは10進法で表されます;あなたは、接頭辞を使用して代替の基数を指定することができます。2進のリテラルは0bから始まります、8進のリテラルは0oから始まります、そして16進のリテラルは0xから始まります。

Decimal literals contain the digits 0 through 9. Binary literals contain 0 and 1, octal literals contain 0 through 7, and hexadecimal literals contain 0 through 9 as well as A through F in upper- or lowercase. 10進のリテラルは、0から9までの桁を含みます。2進のリテラルは01を含みます、8進のリテラルは0から7までを含みます、そして、16進のリテラルは0から9までに加えて大文字または小文字でのAからFまでを含みます。

Negative integers literals are expressed by prepending a minus sign (-) to an integer literal, as in -42. 負整数リテラルは、負符号(-)をリテラル整数の前に付けることによって表されます、-42のように。

Underscores (_) are allowed between digits for readability, but they’re ignored and therefore don’t affect the value of the literal. Integer literals can begin with leading zeros (0), but they’re likewise ignored and don’t affect the base or value of the literal. アンダースコア(_)は、可読性のために桁の間で認められます、しかしそれらは無視されて、それゆえにリテラルの値に影響を及ぼしません。整数リテラルは、いくつかのゼロ(0)を先頭に始まることができます、しかしそれらは同様に無視されて、リテラルの基数または値に影響を及ぼしません。

Unless otherwise specified, the default inferred type of an integer literal is the Swift standard library type Int. The Swift standard library also defines types for various sizes of signed and unsigned integers, as described in Integers. 特に明記しない限り、初期状態で推論されるリテラル整数の型は、スウィフト標準ライブラリ型Intです。整数で記述されるように、スウィフト標準ライブラリはまた、さまざまな大きさの符号つきおよび符号なし整数のために、いろいろな型を定義します。

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-characters opt

binary-digitDigit 0 or 1 アラビア数字の0または1

binary-literal-character binary-digit | _

binary-literal-characters binary-literal-character binary-literal-characters opt

octal-literal 0o octal-digit octal-literal-characters opt

octal-digitDigit 0 through 7 アラビア数字の0から7

octal-literal-character octal-digit | _

octal-literal-characters octal-literal-character octal-literal-characters opt

decimal-literal decimal-digit decimal-literal-characters opt

decimal-digitDigit 0 through 9 アラビア数字の0から9

decimal-digits decimal-digit decimal-digits opt

decimal-literal-character decimal-digit | _

decimal-literal-characters decimal-literal-character decimal-literal-characters opt

hexadecimal-literal 0x hexadecimal-digit hexadecimal-literal-characters opt

hexadecimal-digitDigit 0 through 9, a through f, or A through F アラビア数字の0から9、aからf、またはAからF

hexadecimal-literal-character hexadecimal-digit | _

hexadecimal-literal-characters hexadecimal-literal-character hexadecimal-literal-characters opt

Floating-Point Literals 浮動小数点リテラル

Floating-point literals represent floating-point values of unspecified precision. 浮動小数点リテラルは、不特定精度の浮動小数点値を表します。

By default, floating-point literals are expressed in decimal (with no prefix), but they can also be expressed in hexadecimal (with a 0x prefix). 初期状態で、浮動小数点リテラルは10進において(接頭辞なしで)表されます、しかしそれらはまた、16進法において(0x接頭辞で)表されることができます。

Decimal floating-point literals consist of a sequence of decimal digits followed by either a decimal fraction, a decimal exponent, or both. The decimal fraction consists of a decimal point (.) followed by a sequence of decimal digits. The exponent consists of an upper- or lowercase e prefix followed by a sequence of decimal digits that indicates what power of 10 the value preceding the e is multiplied by. For example, 1.25e2 represents 1.25 x 102, which evaluates to 125.0. Similarly, 1.25e-2 represents 1.25 x 10-2, which evaluates to 0.0125. 10進の浮動小数点リテラルは、一連の10進の桁に、小数部か10進の指数のどちらか、または両方が続くものから成ります。小数部は、小数点(.)に続く一連の10進の桁から成ります。指数は、大文字または小文字の接頭辞e、それに続けてeの前の値が10の何乗を掛けられるかを示す一連の10進の桁から成ります。例えば、1.25e2は1.25 × 102を表します、それは125.0に評価されます。同じように、1.25e-2は1.25 × 10-2を表します、それは0.0125に評価されます。

Hexadecimal floating-point literals consist of a 0x prefix, followed by an optional hexadecimal fraction, followed by a hexadecimal exponent. The hexadecimal fraction consists of a decimal point followed by a sequence of hexadecimal digits. The exponent consists of an upper- or lowercase p prefix followed by a sequence of decimal digits that indicates what power of 2 the value preceding the p is multiplied by. For example, 0xFp2 represents 15 x 22, which evaluates to 60. Similarly, 0xFp-2 represents 15 x 2-2, which evaluates to 3.75. 16進の浮動小数点リテラルは、0x接頭辞、続けて任意の16進の小数部、それに続く16進の指数から成ります。16進の小数部は、小数点に続く一連の16進の桁から成ります。指数は、大文字または小文字のp接頭辞、それに続けてpの前の値が2の何乗を掛けられるかを示す一連の10進の桁から成ります。例えば、0xFp2は15 × 22を表します、それは60に評価されます。同じように、0xFp-2は15 × 2-2を表します、それは3.75に評価されます。

Negative floating-point literals are expressed by prepending a minus sign (-) to a floating-point literal, as in -42.5. 負の浮動小数点数は、浮動小数点リテラルに単項マイナス演算子(-)を前につけることによって表わされます、-42.5のように。

Underscores (_) are allowed between digits for readability, but they’re ignored and therefore don’t affect the value of the literal. Floating-point literals can begin with leading zeros (0), but they’re likewise ignored and don’t affect the base or value of the literal. アンダースコア(_)は、可読性のために桁の間で認められます、しかしそれらは無視されて、それゆえにリテラルの値に影響を及ぼしません。浮動小数点リテラルは、いくつかのゼロ(0)を先頭に始まることができます、しかしそれらは同様に無視されて、リテラルの基数または値に影響を及ぼしません。

Unless otherwise specified, the default inferred type of a floating-point literal is the Swift standard library type Double, which represents a 64-bit floating-point number. The Swift standard library also defines a Float type, which represents a 32-bit floating-point number. 特に明記しない限り、初期状態で推論されるリテラル浮動小数点の型は、スウィフト標準ライブラリ型Doubleです、それは、64ビットの浮動小数点数を表します。スウィフト標準ライブラリはまたFloat型も定義します、それは、32ビットの浮動小数点数を表します。

Grammar of a floating-point literal 浮動小数点リテラルの文法

floating-point-literal decimal-literal decimal-fraction opt decimal-exponent opt

floating-point-literal hexadecimal-literal hexadecimal-fraction opt hexadecimal-exponent

decimal-fraction . decimal-literal

decimal-exponent floating-point-e sign opt decimal-literal

hexadecimal-fraction . hexadecimal-digit hexadecimal-literal-characters opt

hexadecimal-exponent floating-point-p sign opt decimal-literal

floating-point-e e | E

floating-point-p p | P

sign + | -

String Literals 文字列リテラル

A string literal is a sequence of characters surrounded by quotation marks. A single-line string literal is surrounded by double quotation marks and has the following form: 文字列リテラルは、引用符によって囲まれた一連の文字です。単一行の文字列リテラルは、二重引用符によって囲まれて、以下の形式を持ちます:

  1. "characters"

String literals can’t contain an unescaped double quotation mark ("), an unescaped backslash (\), a carriage return, or a line feed. 文字列リテラルは、エスケープされない二重引用符(")、エスケープされないバックスラッシュ(\)、キャリッジリターン、またはラインフィードを含むことが出来ません。

A multiline string literal is surrounded by three double quotation marks and has the following form: 複数行の文字列リテラルは、3つの二重引用符によって囲まれて、以下の形式を持ちます:

  1. """
  2. characters
  3. """

Unlike a single-line string literal, a multiline string literal can contain unescaped double quotation marks ("), carriage returns, and line feeds. It can’t contain three unescaped double quotation marks next to each other. 単一行の文字列リテラルと違い、複数行の文字列リテラルはエスケープされていない二重引用符(")、キャリッジリターン、そしてラインフィードを含むことが出来ません。それは、お互いは別として3つのエスケープされない二重引用符を含むことが出来ません。

The line break after the """ that begins the multiline string literal isn’t part of the string. The line break before the """ that ends the literal is also not part of the string. To make a multiline string literal that begins or ends with a line feed, write a blank line as its first or last line. 複数行文字列リテラルを開始する"""の後のラインブレークは、その文字列の一部ではありません。リテラルの終わりの"""の前のキャリッジリターンまたはラインフィードもまた、その文字列の一部ではありません。ラインフィードで始まるまたは終わる複数行文字列リテラルを作るには、空の行をそれの最初または最後の行に書いてください。

A multiline string literal can be indented using any combination of spaces and tabs; this indentation isn’t included in the string. The """ that ends the literal determines the indentation: Every nonblank line in the literal must begin with exactly the same indentation that appears before the closing """; there’s no conversion between tabs and spaces. You can include additional spaces and tabs after that indentation; those spaces and tabs appear in the string. 複数行文字列リテラルは、空白とタブの任意の組み合わせを使って字下げできます;この字下げはその文字列に含まれません。リテラルの終わりの"""はその字下げを決定します:リテラルの中のあらゆる空でない行は、閉じている"""の前に現れるのと正確に同じ字下げで始まらなければなりません;タブと空白の間の変換はありません。あなたは、追加の空白とタブをその字下げの後に含めることができます;それらの空白とタブは文字列の中に現れます。

Line breaks in a multiline string literal are normalized to use the line feed character. Even if your source file has a mix of carriage returns and line feeds, all of the line breaks in the string will be the same. 複数行文字列リテラルの中のラインブレークは、正規化されて、ラインフィード文字を使用します。たとえあなたのソースファイルがキャリッジリターンとラインフィードの混合を持つとしても、文字列の中のラインブレークの全ては同じになります。

In a multiline string literal, writing a backslash (\) at the end of a line omits that line break from the string. Any whitespace between the backslash and the line break is also omitted. You can use this syntax to hard wrap a multiline string literal in your source code, without changing the value of the resulting string. 複数行文字列リテラルにおいて、バックスラッシュ(\)を行の終わりで書くことはそのラインブレークを文字列から省きます。バックスラッシュとラインブレークの間のあらゆる空白もまた省かれます。あなたは、この構文を使うことで、複数行文字列リテラルをあなたのソースコードにおいてハードラップする(次行に送る)ことが、結果となる文字列の値を変更することなく行えます。

Special characters can be included in string literals of both the single-line and multiline forms using the following escape sequences: 特殊文字は、単一行および複数行形式の両方の文字列リテラルの中に以下のエスケープシーケンスを使って含められます:

  • Null character (\0) null文字(\0
  • Backslash (\\) バックスラッシュ(\\
  • Horizontal tab (\t) 水平タブ(\t
  • Line feed (\n) ラインフィード「字送り文字」(\n
  • Carriage return (\r) キャリッジリターン「復帰文字」(\r
  • Double quotation mark (\") 二重引用符(\"
  • Single quotation mark (\') 一重引用符(\'
  • Unicode scalar (\u{n}), where n is a hexadecimal number that has one to eight digits ユニコード・スカラー(\u{n})、ここでnは16進数で1から8桁を持ちます

The value of an expression can be inserted into a string literal by placing the expression in parentheses after a backslash (\). The interpolated expression can contain a string literal, but can’t contain an unescaped backslash, a carriage return, or a line feed. 式の値は、バックスラッシュ(\)の後の丸括弧の中にその式を置くことによってリテラル文字列に差し込まれることができます。差し込まれた式は、文字列リテラルを含むことができます、しかしエスケープされないバックスラッシュ、キャリッジリターン、またはラインフィードを含むことはできません。

For example, all of the following string literals have the same value: 例えば、以下の文字列リテラルの全ては、同じ値を持ちます:

  1. "1 2 3"
  2. "1 2 \("3")"
  3. "1 2 \(3)"
  4. "1 2 \(1 + 2)"
  5. let x = 3; "1 2 \(x)"

A string delimited by extended delimiters is a sequence of characters surrounded by quotation marks and a balanced set of one or more number signs (#). A string delimited by extended delimiters has the following forms: 拡張区切り記号によって区切られる文字列は、引用符そして均衡のとれたひとまとめの1つ以上のシャープ記号(#)によって囲まれる一連の文字です。拡張区切り記号によって区切られた文字列は、以下の形式を持ちます:

  1. #"characters"#
  2. #"""
  3. characters
  4. """#

Special characters in a string delimited by extended delimiters appear in the resulting string as normal characters rather than as special characters. You can use extended delimiters to create strings with characters that would ordinarily have a special effect such as generating a string interpolation, starting an escape sequence, or terminating the string. 拡張区切り記号によって区切られた文字列は、結果文字列において普通の文字として現れます、特殊文字としてではなく。あなたは拡張区切り記号を使うことで、例えば文字列補間を生成する、エスケープシーケンスを開始する、または文字列を終端するなど、通常は特別な効果を持つ文字で文字列を作成できます。

The following example shows a string literal and a string delimited by extended delimiters that create equivalent string values: 以下の例は、文字列リテラルそして同等の文字列値を作成する拡張区切り記号によって区切られる文字列を示します。

  1. let string = #"\(x) \ " \u{2603}"#
  2. let escaped = "\\(x) \\ \" \\u{2603}"
  3. print(string)
  4. // Prints "\(x) \ " \u{2603}"
  5. print(string == escaped)
  6. // Prints "true"(「true」を出力します)

If you use more than one number sign to form a string delimited by extended delimiters, don’t place whitespace in between the number signs: あなたが1つ以上のシャープ記号を使って拡張区切り記号によって区切られる文字列を形成するならば、空白をシャープ記号の間に置かないでください:

  1. print(###"Line 1\###nLine 2"###) // OK
  2. print(# # #"Line 1\# # #nLine 2"# # #) // Error(エラー)

Multiline string literals that you create using extended delimiters have the same indentation requirements as regular multiline string literals. あなたが拡張区切り記号を使って作成する複数行文字列リテラルは、通常の複数行文字列リテラルと同じ字下げ要件を持ちます。

The default inferred type of a string literal is String. For more information about the String type, see Strings and Characters and String. 初期状態で推論されるリテラル文字列の型は、Stringです。String型についての詳細として、文字列と文字Stringを見てください。

String literals that are concatenated by the + operator are concatenated at compile time. For example, the values of textA and textB in the example below are identical—no runtime concatenation is performed. +演算子によって連結される文字列リテラルは、コンパイル時に連結されます。たとえば、以下の例のtextAtextBの値は同一です ― 実行時連結は行われません。

  1. let textA = "Hello " + "world"
  2. let textB = "Hello world"

Grammar of a string literal 文字列リテラルの文法

string-literal-opening-delimiter extended-string-literal-delimiter opt "

string-literal-closing-delimiter " extended-string-literal-delimiter opt

multiline-string-literal-opening-delimiter extended-string-literal-delimiter opt """

multiline-string-literal-closing-delimiter """ extended-string-literal-delimiter opt

extended-string-literal-delimiter # extended-string-literal-delimiter opt

quoted-text quoted-text-item quoted-text opt

quoted-text-item escaped-character

quoted-text-itemAny Unicode scalar value except ", \, U+000A, or U+000D 何らかのユニコードスカラー値、しかし", \、U+000A、またはU+000Dを除く

multiline-quoted-text multiline-quoted-text-item multiline-quoted-text opt

multiline-quoted-text-item escaped-character

multiline-quoted-text-itemAny Unicode scalar value except \ 任意のユニコードスカラー値、しかし\を除く

multiline-quoted-text-item escaped-newline

interpolated-text interpolated-text-item interpolated-text opt

interpolated-text-item \( expression ) | quoted-text-item

multiline-interpolated-text multiline-interpolated-text-item multiline-interpolated-text opt

multiline-interpolated-text-item \( expression ) | multiline-quoted-text-item

escape-sequence \ extended-string-literal-delimiter

escaped-character escape-sequence 0 | escape-sequence \ | escape-sequence t | escape-sequence n | escape-sequence r | escape-sequence " | escape-sequence '

escaped-character escape-sequence u { unicode-scalar-digits }

unicode-scalar-digitsBetween one and eight hexadecimal digits 1から8桁の16進数

escaped-newline escape-sequence inline-spaces opt line-break

Operators 演算子

The Swift standard library defines a number of operators for your use, many of which are discussed in Basic Operators and Advanced Operators. The present section describes which characters can be used to define custom operators. スウィフト標準ライブラリは、いくつかの演算子をあなたが使うために定義します、その多くは基本の演算子先進の演算子で記述されます。いま目の前の節は、どの文字があつらえの演算子を定義するために使われることができるかを記述します。

Custom operators can begin with one of the ASCII characters /, =, -, +, !, *, %, <, >, &, |, ^, ?, or ~, or one of the Unicode characters defined in the grammar below (which include characters from the Mathematical Operators, Miscellaneous Symbols, and Dingbats Unicode blocks, among others). After the first character, combining Unicode characters are also allowed. あつらえの演算子は、ASCII文字/=-+!*%<>&|^?、および~のうちの1つ、または以下の文法において定義されるユニコード文字のうちの1つで始まります(それはMathematical OperatorsMiscellaneous Symbols、そしてDingbatsユニコードブロックなどからの文字を含みます)。最初の文字の後は、合成ユニコード文字もまた許されます。

You can also define custom operators that begin with a dot (.). These operators can contain additional dots. For example, .+. is treated as a single operator. If an operator doesn’t begin with a dot, it can’t contain a dot elsewhere. For example, +.+ is treated as the + operator followed by the .+ operator. あなたはまた、1つのドット(.)で始まるあつらえの演算子を定義することができます。これらの演算子は、さらなるドットを含むことが可能です。例えば、.+.は単一の演算子とみなされます。ある演算子がドットで始まらないならば、それはどこか他にドットを含むことはできません。例えば、+.+は、+演算子に.+演算子が続くとみなされます。

Although you can define custom operators that contain a question mark (?), they can’t consist of a single question mark character only. Additionally, although operators can contain an exclamation point (!), postfix operators can’t begin with either a question mark or an exclamation point. あなたは疑問符(?)を含むあつらえの演算子を定義できるけれども、それは単一の疑問符文字だけから構成されることはできません。それに加えて、演算子は感嘆符(!)を含むことができますが、接尾辞演算子は、疑問符または感嘆符のどちらかで始まることはできません。

Note 注意

The tokens =, ->, //, /*, */, ., the prefix operators <, &, and ?, the infix operator ?, and the postfix operators >, !, and ? are reserved. These tokens can’t be overloaded, nor can they be used as custom operators. トークン=->///**/.、接頭辞演算子<&、および?、そして接中辞演算子?、そして接尾辞演算子>!、および?は予約されます。これらのトークンは、オーバーロードされることができませんし、それらはあつらえの演算子として使われることもできません。

The whitespace around an operator is used to determine whether an operator is used as a prefix operator, a postfix operator, or an infix operator. This behavior has the following rules: 演算子のまわりの空白は、演算子が接頭辞演算子、接尾辞演算子、または接中辞演算子として使われるかどうかを決定するために使われます。この挙動は以下の規則を持ちます:

  • If an operator has whitespace around both sides or around neither side, it’s treated as an infix operator. As an example, the +++ operator in a+++b and a +++ b is treated as an infix operator. ある演算子が、空白をそのまわりの両側に持つか、そのまわりのどちら側にも持たないならば、それは接中辞演算子とみなされます。例として、+++演算子はa+++ba +++ bにおいて、接中辞演算子とみなされます。
  • If an operator has whitespace on the left side only, it’s treated as a prefix unary operator. As an example, the +++ operator in a +++b is treated as a prefix unary operator. ある演算子が左側だけで空白を持つならば、それは接頭辞単項演算子とみなされます。例として、a +++bにおける+++演算子は、接頭辞単項演算子とみなされます。
  • If an operator has whitespace on the right side only, it’s treated as a postfix unary operator. As an example, the +++ operator in a+++ b is treated as a postfix unary operator. ある演算子が右側だけで空白を持つならば、それは接尾辞単項演算子とみなされます。例として、a+++ bにおける+++演算子は、接尾辞単項演算子とみなされます。
  • If an operator has no whitespace on the left but is followed immediately by a dot (.), it’s treated as a postfix unary operator. As an example, the +++ operator in a+++.b is treated as a postfix unary operator (a+++ .b rather than a +++ .b). ある演算子が左側では空白を持たないが直ちに続けて1つのドット(.)を持つならば、それは接尾辞単項演算子と見なされます。例として、+++演算子はa+++.bにおいて、接尾辞単項演算子(a+++ .ba +++ .bではなく)とみなされます。

For the purposes of these rules, the characters (, [, and { before an operator, the characters ), ], and } after an operator, and the characters ,, ;, and : are also considered whitespace. これらの規則を理由に、演算子の前の文字([、および{、演算子の後の文字)]、および}、そして文字,;、および:は、また、空白と見なされます。

There’s one caveat to the rules above. If the ! or ? predefined operator has no whitespace on the left, it’s treated as a postfix operator, regardless of whether it has whitespace on the right. To use the ? as the optional-chaining operator, it must not have whitespace on the left. To use it in the ternary conditional (? :) operator, it must have whitespace around both sides. 1つのただし書きが、上の規則にあります。あらかじめ定義された演算子!または?が左側に空白を持たないならば、それは接尾辞演算子と見なされます、それが右側に空白を持つかどうかに関係なく。?をオプショナル連鎖演算子として使うには、それは左側に空白を持ってはなりません。それを三項条件(? :)において使うためには、それは両側に空白を持ってはなりません。

In certain constructs, operators with a leading < or > may be split into two or more tokens. The remainder is treated the same way and may be split again. As a result, you don’t need to add whitespace to disambiguate between the closing > characters in constructs like Dictionary<String, Array<Int>>. In this example, the closing > characters aren’t treated as a single token that may then be misinterpreted as a bit shift >> operator. 特定の構造物において、先導する<または>をもつ演算子は、2つ以上のトークンに分割されるでしょう。剰余は、同じ方法で扱われて、そして再び分割されるでしょう。その結果、あなたは空白を加えて閉じ>文字の間の曖昧さをなくす必要はありません、Dictionary<String, Array<Int>>のような構造物においては。この例において、閉じ>文字それらは、単一のトークンとして見なされることはなく、それでビットシフト演算子>>と誤解されることはありません。

To learn how to define new, custom operators, see Custom Operators and Operator Declaration. To learn how to overload existing operators, see Operator Methods. 新しい、あつらえの演算子を定義する方法を学ぶために、あつらえの演算子演算子宣言を見てください。既存の演算子にオーバーロードする方法を学ぶために、演算子メソッドを見てください。

Grammar of operators 演算子の文法

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-headU+00B6, U+00BB, U+00BF, U+00D7, or U+00F7 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-characters opt

dot-operator-head .

dot-operator-character . | operator-character

dot-operator-characters dot-operator-character dot-operator-characters opt

infix-operator operator

prefix-operator operator

postfix-operator operator

About the Language Reference 言語リファレンスについて

Types