2015-09-12 65 views
-1

我正在开发一个使用Swift 2和Xcode 7 beta 6的应用程序。我试图改变一个按钮的标题。我的代码:EXC_BAD_INSTRUCTION错误Swift 2

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

它给了我这样的: 致命错误:意外发现零而展开的可选值 (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() 

} 

} 

回答

0

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

0

我觉得myButton为可选,它的零

+0

我应该在哪里放置“\(options)”?我曾经说过,正常,但我认为它会解决它。它没有... – penatheboss

+0

你可以检查myButton是否为零? – Arsen

+0

你如何检查?我创建了一个从Main.Storyboard到ViewController.Swift的插座... – penatheboss

相关问题