运行我的iOS应用程序时出现EXC_BAD_INST错误

时间:2017-02-24 22:41:59

标签: ios swift

我正在研究一个tic tac toe项目,但它不起作用。我收到的问题是let space = sender.tag。当我点击一个tic tac toe box测试它时会发生什么,它给了我“exc_bad_inst”。任何人都可以帮助我吗?

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var buttonOne: UIButton!
    @IBOutlet weak var buttonTwo: UIButton!
    @IBOutlet weak var buttonThree: UIButton!
    @IBOutlet weak var buttonFour: UIButton!
    @IBOutlet weak var buttonFive: UIButton!
    @IBOutlet weak var buttonSix: UIButton!
    @IBOutlet weak var buttonSeven: UIButton!
    @IBOutlet weak var buttonEight: UIButton!
    @IBOutlet weak var buttonNine: UIButton!

    @IBOutlet weak var gameResult: UILabel!

    var gameState = ["","","",
                     "","","",
                     "","",""
                     ]

    let winningCombos = [[0, 1, 2], [3, 4, 5], [6, 7, 8], //horizontal
                         [0, 3, 6], [1, 4, 7], [2, 5, 8], //vertical
                         [0, 4, 8], [2, 4, 6]] //diagonals

    var gameOver = false
    var turnCount = 0
    var currentPlayer: String!

    func pickFirstPlayer() -> String {
        if arc4random_uniform(2) == 0 {
            currentPlayer = "o"
        } else {
            currentPlayer = "x"
        }
        return currentPlayer
    }

    func toggleNextPlayer() {
        if currentPlayer == "o" {
            currentPlayer = "x"
        } else {
            currentPlayer = "o"
        }
    }

    @IBAction func makeMove(sender: AnyObject) {
        let space = sender.tag
        if (gameState[space] == "" && gameOver == false) {
            let turnImage = UIImage(named: "\(self.currentPlayer).png")
            sender.setImage(turnImage!, forState: .Normal)
            gameState[space] = self.currentPlayer
            self.toggleNextPlayer()
            self.turnCount++
            self.checkForWinner()
        }
    }

    func checkForWinner() {
        var winner = ""
        for combo in winningCombos {
            var row: [String] = []
            for space in combo {
                row.append(gameState[space])
            }
            if (row[0] == row[1] && row[1] == row[2] && row[0] != "") {
                gameOver = true
                winner = row[0].uppercaseString
                gameResult.text = "\(winner) is the winner!"

            }
        }
        if (turnCount == 9 && winner == "") {
            gameResult.text = "It's a tie!"
            gameOver = true
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        pickFirstPlayer()
    }
}

2 个答案:

答案 0 :(得分:0)

您希望使用if letsender投射到UIButton,而不是更改func定义:

@IBAction func makeMove(sender: AnyObject) {
    if let btn = sender as? UIButton {
        let space = btn.tag
        if (gameState[space] == "" && gameOver == false) {
            let turnImage = UIImage(named: "\(self.currentPlayer).png")
            sender.setImage(turnImage!, forState: .Normal)
            gameState[space] = self.currentPlayer
            self.toggleNextPlayer()
            self.turnCount++
            self.checkForWinner()
        }
    } else {
        // something else called this function
    }
}

答案 1 :(得分:-1)

那不应该编译。您已设置动作以使用sender: AnyObject

参数

尝试将发件人参数的类型更改为UIButton

@IBAction func makeMove(sender: UIButton) {

然后编译器将知道发件人包含一个按钮,并且该按钮具有标签属性。

你是如何调用makeMove方法的?