Let's play some Swift Poker Before you can do any sort of card game, you must first write some code to define your deck of cards.

One thing I've noticed in looking at some of Apple's Swift interfaces is that they very much so like to use extensions.  This is probably good.  In the big picture long term, it's a really good way of allowing ever-expanding classes while actually keeping individual source code files quite tidy.

I also wanted to try something out with Swift enums and iterating through every value in them...



The Basics

enum Suit

enum Suit: String {
    case Clubs = "Clubs"
    case Diamonds = "Diamonds"
    case Hearts = "Hearts"
    case Spades = "Spades"
    case __EXHAUST = ""
}


enum Rank

enum Rank: String {
    case Ace = "Ace"
    case Two = "Two"
    case Three = "Three"
    case Four = "Four"
    case Five = "Five"
    case Six = "Six"
    case Seven = "Seven"
    case Eight = "Eight"
    case Nine = "Nine"
    case Ten = "Ten"
    case Jack = "Jack"
    case Queen = "Queen"
    case King = "King"
    case __EXHAUST = ""
}


struct Card

struct Card {
    let suit: Suit
    let rank: Rank
}




The Extensions

Suit: FowardIndexType

extension Suit: ForwardIndexType {
    func successor() -&gt; Suit {
        switch self {
        case .Clubs: return .Diamonds
        case .Diamonds: return .Hearts
        case .Hearts: return .Spades
        case .Spades: return .__EXHAUST
        case .__EXHAUST: return .__EXHAUST
        }
    }
}


Rank: ForwardIndexType

extension Rank: ForwardIndexType {
    func successor() -&gt; Rank {
        switch self {
        case .Ace: return .Two
        case .Two: return .Three
        case .Three: return .Four
        case .Four: return .Five
        case .Five: return .Six
        case .Six: return .Seven
        case .Seven: return .Eight
        case .Eight: return .Nine
        case .Nine: return .Ten
        case .Ten: return .Jack
        case .Jack: return .Queen
        case .Queen: return .King
        case .King: return .__EXHAUST
        case .__EXHAUST: return .__EXHAUST
        }
    }
}


Card: Printable, DebugPrintable

extension Card: Printable, DebugPrintable {
    var description: String {
        get {
            return self.rank.rawValue + " of " + self.suit.rawValue
        }
    }
    var debugDescription: String {
        get {
            return self.rank.rawValue + " of " + self.suit.rawValue
        }
    }
}


Array

extension Array {
    mutating func shuffle() {
        for i in 0..&lt;(count - 1) {
            let j = Int(arc4random_uniform(UInt32(count - i))) + i
            swap(&amp;self[i], &amp;self[j])
        }
    }
}


Int

extension Int {
    func repeat(function: () -&gt; ()) {
        for _ in 1...self {
            function()
        }
    }
}




Example Usage

Constructing the Deck

var deck: [Card] = Array&lt;Card&gt;()
for suit in Suit.Clubs...Suit.Spades {
    for rank in Rank.Ace...Rank.King {
        let newCard = Card(suit: suit, rank: rank)
        deck.append(newCard)
    }
}


Shuffling the Deck

3.repeat({deck.shuffle()})


Printing the entire Deck

for card in deck {
    println(card)
}




Conforming to the ForwardIndexType protocol is what allows the ... and ..&lt; syntax for loops.  The unfortunate downside is that for an enum, we must include one extra value in order to actually iterate through the entire enumeration, per this StackOverflow question.  This seems like a bug that will hopefully be fixed.

I think this is a good foundation for a deck of playing cards in Swift.

Including Jokers might prove difficult.  I'm not sure whether it could be done very from this structure.  And working with extensions means that as we add additional functionality to a card or the enums, we can do it cleanly in new source code files without mucking around with this file.
Bearing in mind that I don't know much Swift...

Your Rank enum could save you a lot of typing if you simply made it a plain old autoincrementing integer enumeration:

enum Rank: Int {
    case Ace = 1, Two, Three, Four, Five, Six, Seven,
    Eight, Nine, Ten, Jack, Queen, King, __EXHAUST
}

extension Rank: ForwardIndexType {
    func successor() -&gt; Rank {
        switch self {
        case .__EXHAUST: return .__EXHAUST
        default: return Rank(rawValue: self.rawValue + 1)!
        }
    }
}

extension Rank: Printable, DebugPrintable {
    var description: String {
        get {
            switch (self) {
            case .Ace: return "Ace"
            case .Jack: return "Jack"
            case .Queen: return "Queen"
            case .King: return "King"
            default: return self.rawValue.description
            }
        }
    }
    var debugDescription: String {
        get { return self.description }
    }
}

for i in Rank.Ace ... .King {
    print(i)
}


When testing out this code, please notice that the Printable protocol doesn't work in Playgrounds at the moment.

Also, of course, shuffling the deck 3 times doesn't make the order of the cards any "more random" than shuffling the deck once. But you probably know that, since you went to the bother of implementing copying a correct shuffling algorithm.

To add jokers, the most "C-like" approach would be to add Suit.Joker and Rank.Joker and leave Card alone.
The most "functional-programming" approach would be to change Card from a struct to an enum with a special case for jokers:

enum Card {
    case Regular(Rank, Suit)
    case Joker
}

var c = Card.Regular(.Ace, .Spades)
var j = Card.Joker