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!
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()
tofunc equalsTapped(sender: AnyObject)
, the exception will be raised becauseequalsTapped
no longer exists, insteadequalsTapped:
now does, and the connection is to the first.)