Nested Types 入れ子にされた型

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. 列挙は、特定のクラスまたは構造体の機能性を支えるためにたびたびつくられます。同じように、あくまでもより複雑な型の文脈内で使用するだけの有用なクラスや構造体を定義することは、便利でありえます。これを達成するために、スウィフトはあなたに入れ子にされた型を定義するのを可能にします、それによってあなたは、補助の列挙、クラス、そして構造体をそれらが支援をする型の定義の範囲内で入れ子にします。

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構造体は、SuitRankと呼ばれる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のどちらかの値を持ちます。この特徴はValuesと呼ばれる構造体で表されます、それは、Rank列挙の内部で入れ子にされます:

  1. struct BlackjackCard {
  2. // nested Suit enumeration(入れ子にされたSuit列挙)
  3. enum Suit: Character {
  4. case spades = "♠", hearts = "♡", diamonds = "♢", clubs = "♣"
  5. }
  6. // nested Rank enumeration(入れ子にされたRank列挙)
  7. enum Rank: Int {
  8. case two = 2, three, four, five, six, seven, eight, nine, ten
  9. case jack, queen, king, ace
  10. struct Values {
  11. let first: Int, second: Int?
  12. }
  13. var values: Values {
  14. switch self {
  15. case .ace:
  16. return Values(first: 1, second: 11)
  17. case .jack, .queen, .king:
  18. return Values(first: 10, second: nil)
  19. default:
  20. return Values(first: self.rawValue, second: nil)
  21. }
  22. }
  23. }
  24. // BlackjackCard properties and methods(BlackjackCardのプロパティとメソッド)
  25. let rank: Rank, suit: Suit
  26. var description: String {
  27. var output = "suit is \(suit.rawValue),"
  28. output += " value is \(rank.values.first)"
  29. if let second = rank.values.second {
  30. output += " or \(second)"
  31. }
  32. return output
  33. }
  34. }

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つの値を持つという事実をカプセル化します、しかし「エース」カードは2つの値を持ちます。Values構造体は、これを表すために2つのプロパティを定義します:

  • first, of type Int first、型Intのもの
  • second, of type Int?, or “optional Int second、型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インスタンスを初期化します。それは、jackqueenking、そして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構造体は、2つのプロパティ ― ranksuitを持ちます。それはまた、descriptionと呼ばれる計算プロパティを定義します、それは、ranksuitに保管される値を使って、カードの名前と説明の値を作ります。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と呼ばれる新しい定数を初期化するために、このイニシャライザを使うことができます:

  1. let theAceOfSpades = BlackjackCard(rank: .ace, suit: .spades)
  2. print("theAceOfSpades: \(theAceOfSpades.description)")
  3. // Prints "theAceOfSpades: suit is ♠, value is 1 or 11"(「theAceOfSpades: 組み札は♠、値は1または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. たとえRankSuitBlackjackCard内部に入れ子にされるとしても、それらの型は前後関係から推論されることができます、なのでこのインスタンスの初期化は、列挙ケース節を参照することがもっぱらそれらのケース節名(.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: 入れ子にされた型をその定義の文脈外で使うために、その名前にそれが入れ子にされている型の名前で接頭辞を付けてください:

  1. let heartsSymbol = BlackjackCard.Suit.hearts.rawValue
  2. // heartsSymbol is "♡"(heartsSymbol は "♡" です)

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. 上の例で、これは、SuitRank、そしてValuesの名前が故意に短いままにしておかれるようにします、なぜなら、それらが定義される文脈によってそれらの名前が自然に修飾されて限定されるからです。

Type Casting 型キャスト

Extensions 拡張