I'm on the initial way of building a calculator. Currently, the code is doing nothing but printing the digits and Pi into the calculator's label when user taps them.
1) Che Code
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var display: UILabel! = nil
var userIsInTheMiddleOfTypeing = false
@IBAction func touchDigit(_ sender: UIButton){
let digit = sender.currentTitle!
if userIsInTheMiddleOfTypeing {
let textCurrentlyInDisplay = display.text!
display.text = textCurrentlyInDisplay + digit
} else {
display.text = digit
}
userIsInTheMiddleOfTypeing = true
}
@IBAction func performOperation(_ sender: UIButton) {
userIsInTheMiddleOfTypeing = false
if let methematicalSymbol = sender.currentTitle {
if methematicalSymbol == "π" {
display.text = String(M_PI) // M_PI
}
}
}
}
2) UI
The touchDigit function is linked to all the digit buttons as shown in the following figure. The display is the UILable
while performOperaton is the PI button
Problem
When I build the code, Xcode first told me the building was success, However, before I could do anything, there is an error popped up as follow
Error Log (copied from the debug area)
2016-07-28 19:30:30.215343 Calculator[11671:208157] bundleid: com.Jeffery.Calculator, enable_level: 0, persist_level: 0, propagate_with_activity: 0
2016-07-28 19:30:30.218796 Calculator[11671:208157] Created DB, header sequence number = 260
2016-07-28 19:30:30.767300 Calculator[11671:208178] subsystem: com.apple.UIKit, category: HIDEvents, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0
2016-07-28 19:30:31.022078 Calculator[11671:208157] Created DB, header sequence number = 260
2016-07-28 19:30:31.350380 Calculator[11671:208157] subsystem: com.apple.BaseBoard, category: MachPort, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0
2016-07-28 19:30:31.388363 Calculator[11671:208159] subsystem: com.apple.FrontBoard, category: Common, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)