嵌套
import UIKit
struct BlackjackCard {
// 嵌套Suit 枚举
enum Suit: Character {
case spades = "♠", hearts = "♡", diamonds = "♢", clubs = "♣︎"
}
// 嵌套Rank 枚举
enum Rank: Int {
case two = 2, three, four, five, six, seven, eight, nine
case jack, queen, king, ace
// 嵌套 Values 结构体
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)
}
}
}
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)")
let heartsSymbol = BlackjackCard.Suit.hearts.rawValue
print(heartsSymbol)
console log 如下
嵌套类型.png
网友评论