1

I am getting an NSException error which I cannot understand since using the updated swift 2. I am aware that the exception is common and in itself doesn't really help. I have included the code I am using. The code is from the start of a calculator application. While this code runs perfectly fine in swift it now offers the exception in the updated swift 2.

class ViewController: UIViewController {
@IBOutlet weak var mainDisplay: UILabel!

@IBAction func appendDigit(sender: UIButton) {
    let digit = sender.currentTitle
    print("digit = \(digit)")
}}

The error is connected to the line print("digit = \(digit)")as far as I can tell. In swift 2 println was removed and combined into print but I am not sure why calling in digit is causing an exception? I am completely new to swift and so any help would be appreciated to help me understand the issue.

Thank you

Mark Searle
  • 55
  • 1
  • 7
  • 1
    Could you send us a print of the NSException error you are receiving? – Mihai Andrei Rustiuc Nov 02 '15 at 15:20
  • 2015-11-02 15:04:20.868 Calculator[7542:2075339] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key maindisplay.' – Mark Searle Nov 02 '15 at 15:26

1 Answers1

3

Your problem is not related to the appendDigit: method. You should check your Storyboard or XIB as there is a connection problem with your IBOutlet, which came probably after your renaming the variable from maindisplay to mainDisplay.

ADDITION:

The solution is to look inside the Storyboard or XIB at the linked UILabel, remove the outlet connection, and set it again using the newly renamed mainDisplay IBOutlet var.

  • Perfect I can see exactly what I did now, I did create a new outlet connection but failed to remove the old one which is what caused the issue. Deleting that old one so only the newer one remained fixed it. Thank you very much – Mark Searle Nov 02 '15 at 15:52