嵌套类型

类型可以嵌套,甚至多层嵌套

struct BlackJackCard {
    enum Suit: Character {
        case spades = "♠", hearts = "♡", diamonds = "♢", clubs = "♣"
    }
    
    enum Rank: Int {
        case two = 2, three, four, five, six, seven, eight, night, ten
        case jack, queen, king, ace
        
        struct Values {
            let first: Int, second: Int?
        }
        
        var value: 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)
         }
      }
    }
    
    let suit: Suit, rank: Rank
    var description: String{
        var outStr = "suit is \(suit.rawValue)"
        outStr += " value is \(rank.value.first)"
        if let _ = rank.value.second{
            outStr += " or \(rank.value.second!)"
        }
        
        return outStr
    }
    
}
var a = BlackJackCard(suit: .clubs, rank: .ace)
print(a.description)

你可能感兴趣的:(swift)