1

I'm beginning to teach myself Swift, I am truly a beginner and I am working on a calculator app just as an intro project. I keep getting a thread error and it is terminating with an uncaught exception of type NSException. I've read in several places that this is usually caused by a missing or wrong connection from the storyboard to the view controller but I triple checked all of my connections and I don't think that is the problem. Here is my code for the view controller, is there an issue with it? I followed a tutorial for the most part.

import UIKit

extension String{
    var doubleValue: Double{
        if let number = NSNumberFormatter().numberFromString(self) {
            return number.doubleValue
        }
        return 0
    }
}

class ViewController: UIViewController {

var isTypingNumber = false
var firstNumber:Double? = 0
var secondNumber:Double? = 0
var operation = ""


@IBOutlet var calculatorDisplay: UILabel!
@IBAction func numberTapped(sender: AnyObject) {
    var number = sender.currentTitle

    if isTypingNumber {
        calculatorDisplay.text = calculatorDisplay.text! + number!!
    } else{
        calculatorDisplay.text = number
        isTypingNumber = true
    }
}
@IBAction func calculationTapped(sender: AnyObject) {
    isTypingNumber = false
    firstNumber = calculatorDisplay.text!.doubleValue
    operation = sender.currentTitle!!
}
@IBAction func equalsTapped(sender: AnyObject) {
    isTypingNumber = false
    var result = 0.0
    secondNumber = calculatorDisplay.text!.doubleValue

    if operation == "+"{
        result = firstNumber! + secondNumber!
    } else if operation == "-"{
        result = firstNumber! - secondNumber!
    } else if operation == "*"{
        result = firstNumber! * secondNumber!
    }
    else if operation == "/"{
        result = firstNumber! / secondNumber!
    }

    calculatorDisplay.text = "\(result)"
}



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

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


}

I really appreciate any help I can get!

1 Answers1

1

Did you happen to change the name of any IBOutlet or IBAction since first installing it into the Storyboard/xib?

Try going to your viewcontroller in Interface Builder, right-click the VC object to see all connected outlets, and check that everything's hooked up and that there aren't any dead links. (i.e. if you renamed func equalsTapped() to func equalsTapped(sender: AnyObject), the exception will be raised because equalsTapped no longer exists, instead equalsTapped: now does, and the connection is to the first.)

apocolipse
  • 619
  • 6
  • 11
  • And be on the lookout for outlets that *are* still connected, but have a teensy tiny white exclamation point on the right edge, which indicates that the outlet no longer exists in the code. – NRitH Aug 24 '15 at 00:52
  • I just double checked that and all of the connections seem good, but I just added the project to github, username hkoumjian, project is Calculator, and I haven't seen any small exclamation points but I will definitely look out for those. Thanks a lot guys – Harry Koumjian Aug 25 '15 at 02:56
  • Nevermind! I just fixed that and it was an extra connection! Thank you so much! Now I have another problem but I'll work on that myself before I complain on here again, thank you! – Harry Koumjian Aug 25 '15 at 02:59
  • Glad to this fixed your issue :). Interface Builder is a tricky part of the build process because even if your VC class no longer contains a property or method, which would cause code trying to call it to fail compiling, Interface Builder will inevitably allow it since it doesn't try to instantiate objects or even make any calls until runtime. Basically what happens is it acts as if you've written the code `objc_msgSend(anObject, Selector("badMethod"))`, which will also compile fine but cause the runtime exception. – apocolipse Aug 25 '15 at 04:31