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) 
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
SLN
  • 4,772
  • 2
  • 38
  • 79
  • Maybe start by using `if let` instead of `!` on optionals. – Phillip Mills Jul 28 '16 at 19:32
  • @IBOutlet weak var display: UILabel! = nil. Why you are setting it to nil? – ldindu Jul 28 '16 at 21:38
  • 1
    You cut the log at the most interesting part :( The next lines were the description of the exception, which in most cases tell you everything you need to know. Also, it is worthwhile to set an exception breakpoint - https://developer.apple.com/library/ios/recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html – Losiowaty Jul 28 '16 at 22:39
  • @Losiowaty I think the log I've cut off is (lldb) – SLN Jul 29 '16 at 11:59
  • @ldindu I figured it out my error, there are two connections attached with the PI button to the viewControler (I should delete one). The code works fine with UILabel! = nil. Why is that? – SLN Jul 29 '16 at 14:02
  • 1
    If the question is solved, accept the answer that solved it, or post your own answer. If you have a new question, post a *new, different* question but don't change the meaning of this existing one. Thanks. – Eric Aya Jul 29 '16 at 14:07
  • 1
    @EricD good points and make sense. I'll do that – SLN Jul 29 '16 at 14:08

2 Answers2

0

I think problem here: '@IBOutlet weak var display: UILabel! = nil'. When you do something with 'display' property you try do it with nil. Just try delete '= nil' after declarating property. Or you can override view controller init method, but as for me, in this situation it is bad way.

Srj0x0
  • 458
  • 3
  • 12
  • Hi, I think that's a good point. I deleted the nil settings, AKA only the UILable!. But the error still exist (BTW, please excuse my late reply) – SLN Jul 29 '16 at 12:03
0

I figured it out my error, there are two connections attached with the PI button to the viewControler (I should delete one).

SLN
  • 4,772
  • 2
  • 38
  • 79
  • For future reference, there is mention of an `NSException` being thrown. This exception object will have valuable information in it which you need to read. Try adding [this breakpoint](http://stackoverflow.com/questions/1163981/how-to-add-a-breakpoint-to-objc-exception-throw) when using Xcode to see it when it's thrown. – Droppy Jul 29 '16 at 14:20