我开始自学 Swift,我是一个真正的初学者,我正在开发一个计算器应用程序,就像一个介绍项目。我不断收到一个线程错误,它以 NSException 类型的未捕获异常终止。我在几个地方读到这通常是由于故事板到视图控制器的连接丢失或错误造成的,但我三重检查了所有连接,我认为这不是问题。这是我的视图控制器代码,它有问题吗?我大部分时间都按照教程进行操作。
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.
}
}
我真的很感激我能得到的任何帮助!