title: Button Crashes App When Touched In Swift
body: <p>I made a button called <code>playAgainButton</code>, which is a button that allows the Tic Tac Toe game reset so you can play again...but this <code>playAgainButton</code> is crashing my app, instead of reseting the game to the default <code>gameState</code>. </p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController {
  
      //All the odd numbers will be noughts &amp; even will be crosses
      var goNumber = 1
      var winner = 0
  
      //0 = empty, 1 = nought, 2 = cross
      var gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0]
  
      let winningCombinations = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]
  
      @IBOutlet weak var button0: UIButton!
      @IBOutlet weak var label: UILabel!
      @IBOutlet weak var playAgain: UIButton!
  
      @IBAction func playAgainButton(sender: AnyObject) {
  
          goNumber = 1
  
          winner = 0
  
          gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0]
  
          label.center = CGPointMake(label.center.x - 400, label.center.y)
  
          playAgain.alpha = 0
  
          var button : UIButton
  
          for var i  = 0; i &lt; 9; i++ {
  
          button = view.viewWithTag(i) as! UIButton
  
          button.setImage(nil, forState: .Normal)
  
          }
  
      }
      @IBAction func buttonPressed(sender: AnyObject) {
  
          var image = UIImage()
  
          if (gameState[sender.tag] == 0) &amp;&amp; winner == 0 {
  
          println(sender.tag)
  
          if (goNumber % 2 == 0 ) {
  
          image = UIImage(named: "cross.png")!
              gameState[sender.tag] = 2
          } else {
  
          image = UIImage(named: "nought.png")!
              gameState[sender.tag] = 1
          }
  
              for combination in winningCombinations {
  
                  if (gameState[combination[0]] == gameState[combination[1]] &amp;&amp; gameState[combination[1]] == gameState[combination[2]]) &amp;&amp; gameState[combination[0]] != 0{
  
                      winner = gameState[combination[0]]
                  }
  
              }
  
              if (winner != 0) {
  
                  println("We have a winner")
  
                  if (winner == 1) {
  
                      label.text = "Noughts has won"
                      label.backgroundColor = UIColor .blueColor()
                  } else {
                      label.text = "Crosses has won"
                      label.backgroundColor = UIColor .blueColor()
                  }
  
                  UIView.animateWithDuration(1, animations: {
                      self.label.center = CGPointMake(self.label.center.x + 400, self.label.center.y)
  
                      self.playAgain.alpha = 1
                  })
              }
  
          goNumber++
          sender.setImage(image, forState: .Normal)
  
          }
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      override func viewDidAppear(animated: Bool) {
          label.center = CGPointMake(label.center.x - 400, label.center.y)
          playAgain.alpha = 0
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/DeiOA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DeiOA.png" alt="Where the crash appears"></a></p>
  
  <p><a href="https://i.stack.imgur.com/Ziz63.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ziz63.png" alt="Error message"></a></p>