0

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

enter image description here

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

enter image description here

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) 
4

2 回答 2

0

我认为这里有问题:'@IBOutlet weak var display: UILabel!= 无'。当您使用“显示”属性执行某些操作时,您尝试使用 nil 执行此操作。在声明属性后尝试删除'= nil'。或者您可以覆盖视图控制器的 init 方法,但对我来说,在这种情况下这是不好的方法。

于 2016-07-28T19:48:03.770 回答
0

我发现了我的错误,PI 按钮连接到 viewControler 有两个连接(我应该删除一个)。

于 2016-07-29T14:09:12.090 回答