2015-09-12 5 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

Куда я должен положить «\ (опции)»? Раньше я ставил. Нормаль, но я думал, что это исправит. Это не ... – penatheboss

+0

Можете ли вы проверить myButton, это он? – Arsen

+0

Как вы это проверить? Я создал выход из Main.Storyboard в ViewController.Swift ... – penatheboss