Nested Types

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

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
   }
}

let theAceOfSpades = BlackjackCard(rank: .ace, suit: .spades)
print(“theAceOfSpades: (theAceOfSpades.description)”)
// Prints “theAceOfSpades: suit is ♠, value is 1 or 11”

let heartsSymbol = BlackjackCard.Suit.hearts.rawValue
// heartsSymbol is “♡”

Comments are closed.

Post Navigation