๐ฐnested types
Swift โฉ types โฉ nested types โฉ
struct Blackjack {
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
}
}
// extension can have nested types
extension Blackjack {
// โญ๏ธ nested type: `Blackjack.Suit`
enum Suit: Character {
case spades = "โ ", hearts = "โก", diamonds = "โข", clubs = "โฃ"
}
// โญ๏ธ nested type: `Blackjack.Rank`
enum Rank: Int {
case two = 2, three, four, five, six, seven, eight, nine, ten
case jack, queen, king, ace
// โญ๏ธ nested type: `Blackjack.Rank.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)
}
}
}
}
Swift โฉ Nested Types
Extensions can define nested types.
can have extension of nested types.
Last updated