title: Looping through enum values in Swift
body: <p>Is it possible to loop through enum values in Swift? Or what is the alternative?</p>
  
  <p>I'm working through Apple's Swift language guide, and I came across this example on enums.</p>
  
  <pre><code>//  EXPERIMENT
  //
  //  Add a method to Card that creates a full deck of cards, 
  //  with one card of each combination of rank and suit.
  
  struct Card {
      var rank: Rank
      var suit: Suit
      func simpleDescription() -&gt; String {
          return "The \(rank.simpleDescription()) of \(suit.simpleDescription())"
      }
  }
  let threeOfSpades = Card(rank: .Three, suit: .Spades)
  let threeOfSpadesDescription = threeOfSpades.simpleDescription()
  
  enum Suit {
      case Spades, Hearts, Diamonds, Clubs
      func simpleDescription() -&gt; String {
          switch self {
          case .Spades:
              return "spades"
          case .Hearts:
              return "hearts"
          case .Diamonds:
              return "diamonds"
          case .Clubs:
              return "clubs"
          }
      }
  }
  
  enum Rank: Int {
      case Ace = 1
      case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten
      case Jack, Queen, King
      func simpleDescription() -&gt; String {
          switch self {
          case .Ace:
              return "ace"
          case .Jack:
              return "jack"
          case .Queen:
              return "queen"
          case .King:
              return "king"
          default:
              return String(self.toRaw())
          }
      }
  }
  </code></pre>
  
  <p>Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. <a href="https://itun.es/us/jEUH0.l">https://itun.es/us/jEUH0.l</a></p>
  
  <p>I've tried the following, but the docs say enums in Swift are not assigned underlying integer values like in C, so I'm probably barking up the wrong tree. </p>
  
  <p>Is there a better way solve this problem?</p>
  
  <pre><code>func deck() -&gt; Card[]{
      var deck: Card[]
      for s in Suit {
          for r in Rank {
              deck += Card(rank: r, suit: s)
          }
      }
      return deck
  }
  
  func deck2() -&gt; Card[]{
      var deck: Card[]
      for var s: Suit = .Spades; s &lt;= .Clubs; s++ {
          for var r: Rank = .Ace; r &lt;= .King; r++ {
              deck += Card(rank: r, suit: s)
          }
      }
      return deck
  }
  </code></pre>