Eric R
Eric R

Reputation: 33

this class is not key value coding-compliant for the key <key> where <key> is not in the code (swift, Xcode 6)

I have typed in a program, the code of which follows below, from a tutorial, trying to get back into coding after a number of years.

When I run the code, everything builds, but then it crashes with subject-line error above, naming a key which does not appear anywhere in my code ("helloText"). Full error output below.

The twist: at one point I had named one of the outlets "helloText" (now named "nameTextField").

I have looked in every file associated with the project I could find, and could not find "helloText". I also tried the menu item "clean."

Thanks for any guidance.

//
//  ViewController.swift
//  ConstructOStraws
//
//  Created by Eric on 11/12/14.
//  Copyright (c) 2014 Eric. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        setupUI()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBOutlet weak var helloLabel: UILabel!
    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var helloButton: UIButton!

    func setupUI(){

        helloLabel.text = "Hello Swift!"
        helloLabel.textColor = UIColor.redColor()
        helloLabel.textAlignment = NSTextAlignment.Center

        nameTextField.placeholder = "Enter your name"

    helloButton.setTitle("Say Hell", forState: .Normal)

    }


    @IBAction func sayHelloAction(sender: AnyObject) {

        let name = nameTextField.text

        if name.isEmpty {

            let alert = UIAlertController(title: "Error", message: "Please Enter a Name.", preferredStyle: UIAlertControllerStyle.Alert)

            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

            self.presentViewController(alert, animated: true, completion: nil)
        } else {

            helloLabel.text = "Hello \(name)"
        }
    }

}

The Errors:

2014-11-12 15:23:10.521 ConstructOStraws[2048:67817] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ConstructOStraws.ViewController 0x7f82e866b420> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key helloText.'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001043b7f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000105efbbb7 objc_exception_throw + 45
    2   CoreFoundation                      0x00000001043b7b79 -[NSException raise] + 9
    3   Foundation                          0x00000001047cf7b3 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
    4   CoreFoundation                      0x0000000104301e80 -[NSArray makeObjectsPerformSelector:] + 224
    5   UIKit                               0x0000000104f08c7d -[UINib instantiateWithOwner:options:] + 1506
    6   UIKit                               0x0000000104d67f98 -[UIViewController _loadViewFromNibNamed:bundle:] + 242
    7   UIKit                               0x0000000104d68588 -[UIViewController loadView] + 109
    8   UIKit                               0x0000000104d687f9 -[UIViewController loadViewIfRequired] + 75
    9   UIKit                               0x0000000104d68c8e -[UIViewController view] + 27
    10  UIKit                               0x0000000104c87ca9 -[UIWindow addRootViewControllerViewIfPossible] + 58
    11  UIKit                               0x0000000104c88041 -[UIWindow _setHidden:forced:] + 247
    12  UIKit                               0x0000000104c9472c -[UIWindow makeKeyAndVisible] + 42
    13  UIKit                               0x0000000104c3f061 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2628
    14  UIKit                               0x0000000104c41d2c -[UIApplication _runWithMainScene:transitionContext:completion:] + 1350
    15  UIKit                               0x0000000104c40bf2 -[UIApplication workspaceDidEndTransaction:] + 179
    16  FrontBoardServices                  0x0000000107a9b2a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
    17  CoreFoundation                      0x00000001042ed53c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    18  CoreFoundation                      0x00000001042e3285 __CFRunLoopDoBlocks + 341
    19  CoreFoundation                      0x00000001042e3045 __CFRunLoopRun + 2389
    20  CoreFoundation                      0x00000001042e2486 CFRunLoopRunSpecific + 470
    21  UIKit                               0x0000000104c40669 -[UIApplication _run] + 413
    22  UIKit                               0x0000000104c43420 UIApplicationMain + 1282
    23  ConstructOStraws                    0x00000001041d661e top_level_code + 78
    24  ConstructOStraws                    0x00000001041d665a main + 42
    25  libdyld.dylib                       0x00000001066d5145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Upvotes: 1

Views: 4478

Answers (1)

ldindu
ldindu

Reputation: 4380

If you have renamed your IBOutlets then chances are you might not deleted the references of old references in your storyboard which might look as follows

Attached Screenshot

Delete the outlet reference with yellow exclamation triangles as shown in the screen. You can get to this screen by right clicking on your view controller object, next to First Responder, in your view controller's storyboard.

Upvotes: 8

Related Questions