title: xcode swift is not refilling array
body: <p>I have a script that loops through rounds of a card game, in this case I have 4 cards and 10 rounds.</p>
  <p>When the cards array is empty I want to refill the cards array and shuffle it again and continue.</p>
  <p>but somehow the refilling doesn't work and I get a error <strong>Index out of range</strong></p>
  <p>this is my code:</p>
  <pre><code>struct card {
      var name: String
      var val: Int
  }
  
  var allCards: [card] = []
  allCards.append(card(name: &quot;card A&quot;, val: 10))
  allCards.append(card(name: &quot;card B&quot;, val: 3))
  allCards.append(card(name: &quot;card C&quot;, val: 5))
  allCards.append(card(name: &quot;card D&quot;, val: 5))
  
  class rounds {
      var roundNumber = 10
      var cards = allCards
      
      func playRounds() {
          switch self.roundNumber {
          case 10:
              self.cards.shuffle()
              self.roundNumber -= 1
          case 1...9:
              if cards.count == 0 {
                  print(&quot;Cards shuffled&quot;)
                  self.cards = allCards
                  self.cards.shuffle()
              }
              self.cards.removeFirst()
              self.roundNumber -= 1
          default:
              print(&quot;End of game&quot;)
          }
      }
  }
  
  var playGame = rounds()
  
  for _ in (0...10) {
      playGame.playRounds()
      print(&quot;Count of cards: \(playGame.cards.count)&quot;)
      print(&quot;Round: \(playGame.roundNumber)&quot;)
      print(&quot;Card name: \(playGame.cards[0].name)\n&quot;)
  </code></pre>
  <p>and this is my result in the debug window:</p>
  <blockquote>
  <p>Count of cards: 4 Round: 9 Card name: card A</p>
  <p>Count of cards: 3 Round: 8 Card name: card D</p>
  <p>Count of cards: 2 Round: 7 Card name: card B</p>
  <p>Count of cards: 1 Round: 6 Card name: card C</p>
  <p>Count of cards: 0 Round: 5 Fatal error: Index out of range: file</p>
  </blockquote>
  <p>in the if statement in case 1...9 I have a <strong>print(&quot;Cards Shuffled&quot;)</strong> that I don't see in de debug window, to me it looks that there is a problem in that if statement, but I don't understand what's wrong with this.</p>
  <p>btw. the for loop at the bottom is for testing purpose, when this script works I'll assign this script to a button</p>