Home>

I am confused with the error displayed below.

It occurred when the simulator was started.
Please give us your opinion!

XcodeVersion 11.5 (11E608c)


P.S.

Terminating app due to uncaught exception'NSUnknownKeyException', reason:'[setValue: forUndefinedKey:]: this class is not key value coding-compliant for the key button.'

Below is the full source code.

import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var label: UILabel!
    override func viewDidLoad () {
        super.viewDidLoad ()
        // Do any additional setup after loading the view.
    }
    @IBAction func button (_ sender: UIButton) {
        label.text = textField.text
    }
}
  • Answer # 1

    This error is basically an Outlet connection error.

    Often, when connecting from StoryBoard with an Outlet, the variable name is misspelled and you will need to manually correct it later.

    Even if you modify the variable name in the code, the information will not be reflected in other related files, so even if it is troublesome, it is the most reliable method to delete all the related information and then reconnect.

    If you open the Connections Inspector with the StoryBoard open, you can see the connection relationship between the StoryBoard and the code.

    The icon design is different depending on the version of Xcode, but in the screenshot above, the blue icon on the far right is the Connections Inspector.

    If you click this, the connection relationship will appear as above, but if there is a connection error, a yellow triangle (icon like ⚠️) will be displayed.

    This is likely to be the cause of the run-time error, so you can probably fix it by clicking the cross to break the relationship and reconnect.