Swift NSUnknownKeyException

mdobrenko picture mdobrenko · Jun 5, 2015 · Viewed 12.2k times · Source

Started learning Swift a day or two ago. I run into this error more times than I'd like -- essentially, I realize that it has something to do with the IBOutlets. Removing all outlets and buttons/objects in the storyboard, and manually re-adding them seems to do the trick.

In the future if this error occurs, and I'm working on a large project, I would like to know how to diagnose which IBOutlet it is, or whatever it may be that is causing this.

I have a copy of the project on GitHub here if anyone wants to take a look and leave a few words of advice.

Thanks a ton in advance!

2015-06-04 22:24:31.607 Controlling The Keyboard[1343:16382] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Controlling_The_Keyboard.ViewController 0x7f80896418b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key enterButton.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000104855c65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001063c0bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x00000001048558a9 -[NSException raise] + 9
    3   Foundation                          0x0000000104c73b53 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
    4   CoreFoundation                      0x000000010479dd50 -[NSArray makeObjectsPerformSelector:] + 224
    5   UIKit                               0x00000001053cc52b -[UINib instantiateWithOwner:options:] + 1506
    6   UIKit                               0x0000000105224718 -[UIViewController _loadViewFromNibNamed:bundle:] + 242
    7   UIKit                               0x0000000105224d08 -[UIViewController loadView] + 109
    8   UIKit                               0x0000000105224f79 -[UIViewController loadViewIfRequired] + 75
    9   UIKit                               0x000000010522540e -[UIViewController view] + 27
    10  UIKit                               0x00000001051402c9 -[UIWindow addRootViewControllerViewIfPossible] + 58
    11  UIKit                               0x000000010514068f -[UIWindow _setHidden:forced:] + 247
    12  UIKit                               0x000000010514ce21 -[UIWindow makeKeyAndVisible] + 42
    13  UIKit                               0x00000001050f0457 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2732
    14  UIKit                               0x00000001050f31de -[UIApplication _runWithMainScene:transitionContext:completion:] + 1349
    15  UIKit                               0x00000001050f20d5 -[UIApplication workspaceDidEndTransaction:] + 179
    16  FrontBoardServices                  0x0000000107ed05e5 __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 21
    17  CoreFoundation                      0x000000010478941c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    18  CoreFoundation                      0x000000010477f165 __CFRunLoopDoBlocks + 341
    19  CoreFoundation                      0x000000010477ef25 __CFRunLoopRun + 2389
    20  CoreFoundation                      0x000000010477e366 CFRunLoopRunSpecific + 470
    21  UIKit                               0x00000001050f1b42 -[UIApplication _run] + 413
    22  UIKit                               0x00000001050f4900 UIApplicationMain + 1282
    23  Controlling The Keyboard            0x0000000104654f37 main + 135
    24  libdyld.dylib                       0x0000000106b18145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Answer

Shamsudheen TK picture Shamsudheen TK · Jun 5, 2015

Adding the below line in the ViewController will fix your issue.

@IBOutlet weak var enterButton: UIButton! 

The error is happening because of the ViewController is connected with three IBOutlet's in the storyboard file.

You have written only two IBOutlet's in the ViewController.swift file as IBOutlet's. You missed to write enterButton IBOutlet. That's it.

enter image description here

enter image description here