Swift 错误线程 1:EXC_BAD_INSTRUCITON

Swift error Thread 1: EXC_BAD_INSTRUCITON

嗨,我刚刚学习 swift,我需要一些帮助。当我 运行 我的应用程序崩溃时。我收到错误

player1 = Player(name: "\(playerNames[0])", hp: 110, attackPwr: 10)

它说:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

我尝试将它放在 if 语句中,即

if playerNames.count == 2 {
    player1 = Player(name: "\(playerNames[0])", hp: 110, attackPwr: 10)
}

然后加载了我的应用程序,但是当我按下 player1AttackBtn on 该行时我遇到了同样的错误。

@IBAction func player1AttackBtn(sender: AnyObject)

如有任何帮助,我们将不胜感激!提前致谢!!!

Parent Class

import Foundation

class Character {
    private var _hp: Int = 100
    private var _attackPwr: Int = 10

    var attackPwr: Int  {
        get {
            return _attackPwr
        }
    }

    var hp: Int {
        get {
            return _hp
        }
    }

    var isAlive: Bool {
        get {
            if hp <= 0 {
                return false
            } else {
                return true
            }
        }
    }

    init(startingHp: Int, attackPwr: Int) {
        self._hp = startingHp
        self._attackPwr = attackPwr
    }

    func attemptAttack(attackPwr: Int) -> Bool {
        self._hp -= attackPwr

        return true
    }

}

玩家Class

import Foundation

class Player: Character {
    private var _name = "Player"

    var name: String {
        get {
            return _name
        }
    }

    convenience init(name: String, hp: Int, attackPwr: Int) {
        self.init(startingHp: hp, attackPwr: attackPwr)
        _name = name
    }


}

视图控制器

import UIKit
import AVFoundation

//var bgMusicAudio = AVAudioPlayer!
var player1: Player!
var player2: Player!


class ViewController: UIViewController {

    var playerNames = [String]()

    @IBOutlet weak var playerOne: UIImageView!
    @IBOutlet weak var playerTwo: UIImageView!
    @IBOutlet weak var nameLbl: UITextField!
    @IBOutlet weak var textBtn: UIButton!
    @IBOutlet weak var printLbl: UILabel!
    @IBOutlet weak var fightBtn: UIButton!
    @IBOutlet weak var player1Attack: UIButton!
    @IBOutlet weak var player2Attack: UIButton!
    @IBOutlet weak var attackLbl1: UILabel!
    @IBOutlet weak var attackLbl2: UILabel!
    @IBOutlet weak var player1HP: UILabel!
    @IBOutlet weak var player2HP: UILabel!

    var bgMusic = AVAudioPlayer!()

    override func viewDidLoad() {
        super.viewDidLoad()


        player1 = Player(name: "\(playerNames[0])", hp: 110, attackPwr: 10)
        player2 = Player(name: "\(playerNames[1])", hp: 110, attackPwr: 10)

        player1HP.text = "\(player1.hp)"
        player2HP.text = "\(player2.hp)"



        let bgMusicPath = NSBundle.mainBundle().pathForResource("8BitTrack", ofType: "wav")
        let bgMusicURL = NSURL(fileURLWithPath: bgMusicPath!)

        do {
            try bgMusic = AVAudioPlayer(contentsOfURL: bgMusicURL)
            bgMusic.prepareToPlay()

        } catch let err as NSError {
            print(err.debugDescription)
        }

        bgMusic.numberOfLoops = -1
        bgMusic.play()

        printLbl.text = "Player one, what is your name?"

}




    @IBAction func player1AttackBtn(sender: AnyObject) {

        if player2.attemptAttack(player1.attackPwr) {
            printLbl.text = "Attacked \(player2) for \(player1.attackPwr) HP"
            player2HP.text = "\(player2.hp)"

        } else {
            printLbl.text = "Attack was unsuccessful!"
        }

        if !player2.isAlive {
            player2HP.text = ""
            printLbl.text = "\(player1.name) killed \(player2.name)."
            playerTwo.hidden = true            
        } 
    }


    @IBAction func player2AttackBtn(sender: AnyObject) {
    }




    @IBAction func doneBtn(sender: AnyObject) {
        if let name = nameLbl.text {

            playerNames.append(name)

            nameLbl.text = ""

            printLbl.text = "Player two, what is your name?"

            if playerNames.count == 2 {
                nameLbl.hidden = true
                textBtn.hidden = true
                printLbl.text = "Welcome \(playerNames[0]) and \(playerNames[1])"
                fightBtn.hidden = false

            }

        }

    }

    @IBAction func fightBtnPressed(sender: AnyObject) {

        player1Attack.hidden = false
        player2Attack.hidden = false
        attackLbl1.hidden = false
        attackLbl2.hidden = false
        fightBtn.hidden = true
        printLbl.text = "Press attack to attack!"
        player1HP.hidden = false
        player2HP.hidden = false
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

好吧,你在 viewDidLoad 中调用它,但你在 @IBAction func doneBtn(sender: AnyObject) 中将名称附加到 playerNames。当你第一次调用它时(也是唯一一次因为应用程序崩溃)数组是空的。