1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| enum Rank: Int { case ace = 1 case two, three, four, five, six, seven, eight, nine, ten case jack, queen, king func simpleDescription() -> String { switch self { case .ace: return "ace" case .jack: return "jack" case .queen: return "queen" case .king: return "king" default: return String(self.rawValue) } } } let ace = Rank.ace let aceRawValue = ace.rawValue
func compareRankByRawValue(v1: Rank, v2: Rank) -> Int { return v1.rawValue - v2.rawValue } print(compareRankByRawValue(v1: Rank.ace, v2: Rank.queen))
if let convertedRank = Rank(rawValue: 3) { let _ = convertedRank.simpleDescription() }
enum Suit { case spades, hearts, diamonds, clubs func simpleDescription() -> String { switch self { case .spades: return "spades" case .hearts: return "hearts" case .diamonds: return "diamonds" case .clubs: return "clubs" } } func color() -> String { switch self { case .spades, .clubs: return "black" case .hearts, .diamonds: return "heart" } } } let hearts = Suit.hearts let heartsDescription = hearts.simpleDescription()
enum ServerResponse { case result(String, String) case failure(String) case offline(Void) }
let success = ServerResponse.result("6:00 am", "8:09 pm") let failure = ServerResponse.failure("Out of cheese.")
switch success { case let .result(sunrise, sunset): print("Sunrise is at \(sunrise) and sunset is at \(sunset)") case let .failure(message): print("Failure... \(message)") case .offline(): print("Your device is offline...") }
struct Card { var rank: Rank var suit: Suit func simpleDescription() -> String { return "The \(rank.simpleDescription()) of \(suit.simpleDescription())" } } let threeOfSpades = Card(rank: .three, suit: .spades) let threeOfSpadesDescription = threeOfSpades.simpleDescription()
func getPokerCards() -> [Card] { var cards = [Card]() for rank in 1...13 { cards.append(Card(rank: Rank(rawValue: rank) ?? .ace, suit: .spades)) cards.append(Card(rank: Rank(rawValue: rank) ?? .ace, suit: .hearts)) cards.append(Card(rank: Rank(rawValue: rank) ?? .ace, suit: .clubs)) cards.append(Card(rank: Rank(rawValue: rank) ?? .ace, suit: .diamonds)) } for card in cards { print(card.simpleDescription()) } return cards }
|