入れ子になった型
日本語を消す 英語を消す下記URLから引用し、日本語訳をつけてみました。
https://docs.swift.org/swift-book/documentation/the-swift-programming-language/nestedtypes
Define types inside the scope of another type.
別の型のスコープ内で型を定義します。
Enumerations are often created to support a specific class or structure’s functionality. Similarly, it can be convenient to define utility classes and structures purely for use within the context of a more complex type. To accomplish this, Swift enables you to define nested types, whereby you nest supporting enumerations, classes, and structures within the definition of the type they support.
列挙型は多くの場合、特定のクラスまたは構造体の機能をサポートするために作成されます。 同様に、より複雑な型のコンテキスト内でのみ使用するためにユーティリティ クラスと構造体を定義すると便利な場合があります。 これを実現するために、Swift ではネストされた型を定義できます。これにより、サポートする列挙型、クラス、構造体を、サポートする型の定義内でネストできます。
To nest a type within another type, write its definition within the outer braces of the type it supports. Types can be nested to as many levels as are required.
ある型を別の型内にネストするには、その型がサポートする型の外側の中括弧内にその定義を記述します。 型は、必要なレベルまでネストできます。
Nested Types in Action
ネストされた型の動作
The example below defines a structure called BlackjackCard, which models a playing card as used in the game of Blackjack. The BlackjackCard structure contains two nested enumeration types called Suit and Rank.
以下の例では、ブラックジャックのゲームで使用されるトランプをモデル化する BlackjackCard という構造を定義します。 BlackjackCard 構造には、Suit と Rank と呼ばれる 2 つの入れ子になった列挙型が含まれています。
In Blackjack, the Ace cards have a value of either one or eleven. This feature is represented by a structure called Values, which is nested within the Rank enumeration:
ブラックジャックでは、エース カードの値は 1 または 11 です。 この機能は、Rank 列挙内にネストされた Values と呼ばれる構造によって表されます。
struct BlackjackCard {
// nested Suit enumeration
enum Suit: Character {
case spades = "♠", hearts = "♡", diamonds = "♢", clubs = "♣"
}
// nested Rank enumeration
enum Rank: Int {
case two = 2, three, four, five, six, seven, eight, nine, ten
case jack, queen, king, ace
struct Values {
let first: Int, second: Int?
}
var values: Values {
switch self {
case .ace:
return Values(first: 1, second: 11)
case .jack, .queen, .king:
return Values(first: 10, second: nil)
default:
return Values(first: self.rawValue, second: nil)
}
}
}
// BlackjackCard properties and methods
let rank: Rank, suit: Suit
var description: String {
var output = "suit is \(suit.rawValue),"
output += " value is \(rank.values.first)"
if let second = rank.values.second {
output += " or \(second)"
}
return output
}
}
The Suit enumeration describes the four common playing card suits, together with a raw Character value to represent their symbol.
Suit の列挙は、4 つの一般的なトランプのスートと、そのシンボルを表す生のCharacter 値を説明します。
The Rank enumeration describes the thirteen possible playing card ranks, together with a raw Int value to represent their face value. (This raw Int value isn’t used for the Jack, Queen, King, and Ace cards.)
Rank 列挙は、13 の可能なトランプ ランクと、その額面を表す生の Int 値を記述します。 (この生の Int 値は、ジャック、クイーン、キング、エースのカードには使用されません。)
As mentioned above, the Rank enumeration defines a further nested structure of its own, called Values. This structure encapsulates the fact that most cards have one value, but the Ace card has two values. The Values structure defines two properties to represent this:
上で述べたように、Rank 列挙は、Values と呼ばれる独自のさらにネストされた構造を定義します。 この構造は、ほとんどのカードには 1 つの値があるが、Ace カードには 2 つの値があるという事実をカプセル化しています。 Values 構造では、これを表す 2 つのプロパティが定義されています。
first, of typeInt- まず、
Int型の second, of typeInt?, or “optionalInt”- 2 番目、Int 型、または「オプションの Int」
Rank also defines a computed property, values, which returns an instance of the Values structure. This computed property considers the rank of the card and initializes a new Values instance with appropriate values based on its rank. It uses special values for jack, queen, king, and ace. For the numeric cards, it uses the rank’s raw Int value.
Rank は、Values 構造のインスタンスを返す計算プロパティ、valuesも定義します。 この計算されたプロパティはカードのランクを考慮し、そのランクに基づいた適切な値で新しい Values インスタンスを初期化します。 jack、queen、king、aceに特別な値を使用します。 数値カードの場合は、ランクの生の Int 値が使用されます。
The BlackjackCard structure itself has two properties — rank and suit. It also defines a computed property called description, which uses the values stored in rank and suit to build a description of the name and value of the card. The description property uses optional binding to check whether there’s a second value to display, and if so, inserts additional description detail for that second value.
BlackjackCard の構造自体には、rank とsuitという 2 つのプロパティがあります。 また、description と呼ばれる計算プロパティも定義します。このプロパティは、rank と suit に保存されている値を使用して、カードの名前と値の説明を作成します。 description プロパティは、オプションのバインディングを使用して、表示する 2 番目の値があるかどうかを確認し、存在する場合は、その 2 番目の値の追加の説明の詳細を挿入します。
Because BlackjackCard is a structure with no custom initializers, it has an implicit memberwise initializer, as described in Memberwise Initializers for Structure Types. You can use this initializer to initialize a new constant called theAceOfSpades:
BlackjackCard はカスタム初期化子を持たない構造体であるため、「構造型のメンバーごとの初期化子」で説明されているように、暗黙的なメンバーごとの初期化子があります。 このイニシャライザを使用して、theAceOfSpades という新しい定数を初期化できます。
let theAceOfSpades = BlackjackCard(rank: .ace, suit: .spades)
print("theAceOfSpades: \(theAceOfSpades.description)")
// Prints "theAceOfSpades: suit is ♠, value is 1 or 11"
Even though Rank and Suit are nested within BlackjackCard, their type can be inferred from context, and so the initialization of this instance is able to refer to the enumeration cases by their case names (.ace and .spades) alone. In the example above, the description property correctly reports that the Ace of Spades has a value of 1 or 11.
Rank と Suite は BlackjackCard 内にネストされていますが、そのタイプはコンテキストから推測できるため、このインスタンスの初期化ではケース名 (.ace と .spades) だけで列挙ケースを参照できます。 上の例では、description プロパティは、スペードのエースの値が 1 または 11 であることを正しく報告しています。
Referring to Nested Types
ネストされた型の参照
To use a nested type outside of its definition context, prefix its name with the name of the type it’s nested within:
入れ子になった型をその定義コンテキストの外で使用するには、その名前の前に、入れ子になっている型の名前を付けます。
let heartsSymbol = BlackjackCard.Suit.hearts.rawValue
// heartsSymbol is "♡"
For the example above, this enables the names of Suit, Rank, and Values to be kept deliberately short, because their names are naturally qualified by the context in which they’re defined.
上記の例では、これにより、Suit、Rank、Values の名前を意図的に短くすることができます。これは、それらの名前が定義されているコンテキストによって自然に修飾されるためです。
