Search code examples
xcodeswiftswift2nsexception

Swift 2 terminating with uncaught exception of type NSException


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


Solution

  • 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.