Swift Language Guide Documentation(ネストタイプ部品)の学習

10273 ワード

Nested Types


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.

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.
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:
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
    }
}
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:
(前述したように、ネストされた構造を定義し、「順序付き値」と呼ばれます.この構造は、ほとんどのカードに1つの値がカプセル化されていますが、ACEカードには2つの値があります.値構造は、その値を表す2つの属性を定義します.)
  • first, of type Int
  • second, of type Int?, or “optional Int”
  • Rank also defines a computed property, values, which returns an instance of the Values structure.
    The BlackjackCard structure itself has two properties—rank and suit. 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.
    Because BlackjackCard is a structure with no custom initializers, it has an implicit memberwise initializer, as described in Memberwise Initializers for Structure Types .
    let theAceOfSpades = BlackjackCard(rank: .ace, suit: .spades)
    print("theAceOfSpades: \(theAceOfSpades.description)")
    // Prints "theAceOfSpades: suit is ♠, value is 1 or 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 "♡"