EXC_BAD_INSTRUCTION错误Swift 2

时间:2015-09-12 14:07:07

标签: uibutton swift2 ios9 xcode7-beta6 exc-bad-instruction

我正在使用Swift 2和Xcode 7 beta 6开发应用程序。我正在尝试更改按钮的标题。我的代码:

 print(randomNumber)
 myButton.setTitle("\(randomNumber)", forState: UIControlState.Normal)

它给了我这个: 致命错误:在展开Optional值时意外发现nil (lldb)

然而,当我打印randomNumber时,我得到了我想要的随机整数。我的代码中我做错了什么?或者仅仅是测试版的问题?

我的完整代码:

//
//  ViewController.swift
//  Math Practice
//
//  Created by Pranav Wadhwa on 9/6/15.
//  Copyright © 2015 Pranav Wadhwa. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

var firstNumber = Int()
var secondNumber = Int()
var sign = String()
let arrayOfSigns = ["add", "subtract", "divide", "mulitply"]
var correctAnswer = Int()
var percentage = Int()
var buttonWithCorrectAnswer = Int()
var correct = Int()
var total = Int()
var timer = NSTimer()
var seconds = 0
var minutes = 0
var inSession = false
var firstRandomAnswer = Int()
var secondRandomAnswer = Int()
var thirdRandomAnswer = Int()
var fourthRandomAnswer = Int()



@IBOutlet var timerLabel: UILabel!
@IBOutlet var questionLabel: UILabel!
@IBOutlet var correctLabel: UILabel!
@IBOutlet var percentageLabel: UILabel!


@IBOutlet var button1: UIButton!
@IBOutlet var button2: UIButton!
@IBOutlet var button3: UIButton!
@IBOutlet var button4: UIButton!
@IBOutlet var startPause: UIButton!


@IBAction func startPause(sender: AnyObject) {
    if inSession == false {
        createQuestion()
        inSession = true
        print(startPause)
        startPause.setTitle("Pause", forState: UIControlState.Normal)
        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
    } else if inSession == true {
        inSession = false
        startPause.setTitle("Start", forState: UIControlState.Normal)
        questionLabel.text = ""
        button1.setTitle("", forState: UIControlState.Normal)
        button2.setTitle("", forState: UIControlState.Normal)
        button3.setTitle("", forState: UIControlState.Normal)
        button4.setTitle("", forState: UIControlState.Normal)
        timer.invalidate()
    }
}

@IBAction func reset(sender: AnyObject) {

    seconds = 0
    minutes = 0
    correct = 0
    percentage = 0
    total = 0
    correctLabel.text = "0/0"
    timerLabel.text = "00:00"
    percentageLabel.text = "0%"
    questionLabel.text = ""
    button1.setTitle("", forState: UIControlState.Normal)
    button2.setTitle("", forState: UIControlState.Normal)
    button3.setTitle("", forState: UIControlState.Normal)
    button4.setTitle("", forState: UIControlState.Normal)
    timer.invalidate()
    inSession = false

}



@IBAction func button1(sender: AnyObject) {
    if buttonWithCorrectAnswer == 1 {
        correct += 1
    }
    total += 1
    createQuestion()
}
@IBAction func button2(sender: AnyObject) {
    if buttonWithCorrectAnswer == 2 {
        correct += 1
    }
    total += 1
    createQuestion()
}
@IBAction func button3(sender: AnyObject) {
    if buttonWithCorrectAnswer == 3 {
        correct += 1
    }
    total += 1
    createQuestion()
}
@IBAction func button4(sender: AnyObject) {
    if buttonWithCorrectAnswer == 4 {
        correct += 1
    }
    total += 1
    createQuestion()
}

func updateTime() {

    seconds += 01
    if seconds == 60 {
        minutes++
        seconds = 0
    }
    if seconds < 10 {
        timerLabel.text = "\(minutes):0\(seconds)"
    } else {
        timerLabel.text = "\(minutes):\(seconds)"
    }

}

func createQuestion() {
    if correct != 0 && total != 0 {
        percentage = correct / total
    }
    correctLabel.text = "\(correct)/\(total)"
    percentageLabel.text = "\(percentage)%"
    firstNumber = Int(arc4random_uniform(39))
    secondNumber = Int(arc4random_uniform(39))
    firstNumber -= 20
    secondNumber -= 20
    let i = Int(arc4random_uniform(4))
    sign = arrayOfSigns[i]
    if sign == "divide" {
        firstNumber = Int(arc4random_uniform(39))
        secondNumber = Int(arc4random_uniform(39))
        firstNumber -= 20
        secondNumber -= 20
    }

    if sign == "add" {
        questionLabel.text = "\(firstNumber) + \(secondNumber)"
        correctAnswer = firstNumber + secondNumber
    }
    if sign == "subtract" {
        questionLabel.text = "\(firstNumber) - \(secondNumber)"
        correctAnswer = firstNumber - secondNumber
    }
    if sign == "divide" {
        firstNumber = firstNumber * secondNumber
        questionLabel.text = "\(firstNumber) / \(secondNumber)"
        correctAnswer = firstNumber / secondNumber
    }
    if sign == "multiply" {
        questionLabel.text = "\(firstNumber) * \(secondNumber)"
        correctAnswer = firstNumber * secondNumber
    }
    createAnswers()
}

func createAnswers() {
    buttonWithCorrectAnswer = Int(arc4random_uniform(4) + 1)

    //Insert Correct Answer
    if buttonWithCorrectAnswer == 1 {
        button1.setTitle("\(correctAnswer)", forState: UIControlState.Normal)
    } else if buttonWithCorrectAnswer == 2 {
        button2.setTitle("\(correctAnswer)", forState: UIControlState.Normal)
    } else if buttonWithCorrectAnswer == 3 {
        button3.setTitle("\(correctAnswer)", forState: UIControlState.Normal)
    } else if buttonWithCorrectAnswer == 4 {
        button4.setTitle("\(correctAnswer)", forState: UIControlState.Normal)
    }

    //Setup Fake Answer
    if sign == "add" || sign == "subtract" || sign == "divide" {

        firstRandomAnswer = Int(arc4random_uniform(79))
        firstRandomAnswer -= 40
        print("Hello wold")
        print(firstRandomAnswer)

        secondRandomAnswer = Int(arc4random_uniform(79))
        secondRandomAnswer -= 40
        while secondRandomAnswer == firstRandomAnswer {
            secondRandomAnswer = Int(arc4random_uniform(79))
            secondRandomAnswer -= 40
        }
        thirdRandomAnswer = Int(arc4random_uniform(79))
        thirdRandomAnswer -= 40
        while thirdRandomAnswer == secondRandomAnswer || thirdRandomAnswer == firstRandomAnswer {
            thirdRandomAnswer = Int(arc4random_uniform(79))
            thirdRandomAnswer -= 40
        }

        fourthRandomAnswer = Int(arc4random_uniform(79))
        fourthRandomAnswer -= 40
        while fourthRandomAnswer == thirdRandomAnswer || fourthRandomAnswer == secondRandomAnswer || fourthRandomAnswer == firstRandomAnswer {
            fourthRandomAnswer = Int(arc4random_uniform(79))
            fourthRandomAnswer -= 40
        }
    }
    if sign == "multiply" {

        firstRandomAnswer = Int(arc4random_uniform(799))
        firstRandomAnswer -= 400

        secondRandomAnswer = Int(arc4random_uniform(799))
        secondRandomAnswer -= 400
        while secondRandomAnswer == firstRandomAnswer {
            secondRandomAnswer = Int(arc4random_uniform(799))
            secondRandomAnswer -= 400
        }

        thirdRandomAnswer = Int(arc4random_uniform(799))
        thirdRandomAnswer -= 400
        while thirdRandomAnswer == secondRandomAnswer || thirdRandomAnswer == firstRandomAnswer {
            thirdRandomAnswer = Int(arc4random_uniform(799))
            thirdRandomAnswer -= 400
        }

        fourthRandomAnswer = Int(arc4random_uniform(799))
        fourthRandomAnswer -= 400
        while fourthRandomAnswer == thirdRandomAnswer || fourthRandomAnswer == secondRandomAnswer || fourthRandomAnswer == firstRandomAnswer {
            fourthRandomAnswer = Int(arc4random_uniform(799))
            fourthRandomAnswer -= 400
        }
    }


    //Insert Fake Answers
    if buttonWithCorrectAnswer != 1 {
        button1.setTitle("\(firstRandomAnswer)", forState: UIControlState.Normal)
    }
    if buttonWithCorrectAnswer != 2 {
        button2.setTitle("\(secondRandomAnswer)", forState: UIControlState.Normal)
    }
    if buttonWithCorrectAnswer != 3 {
        button3.setTitle("\(thirdRandomAnswer)", forState: UIControlState.Normal)
    }
    if buttonWithCorrectAnswer != 4 {
        button4.setTitle("\(fourthRandomAnswer)", forState: UIControlState.Normal)
    }
}

@IBOutlet var backgroundImage: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    var image = arc4random_uniform(2)
    image++
    backgroundImage.image = UIImage(named: "background_" + String(image))

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

}

2 个答案:

答案 0 :(得分:0)

我认为myButton是Optional,它是nil

答案 1 :(得分:0)

我的按钮设为零。我通过重新连接插座来解决这个问题。