Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift NSUnknownKeyException

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) 
like image 937
mdobrenko Avatar asked Jun 05 '15 03:06

mdobrenko


2 Answers

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

like image 113
Shamsudheen TK Avatar answered Oct 11 '22 11:10

Shamsudheen TK


I used to meet such issues. I think the best way is to read the error log and figure out what's going on.

Your error message is saying

this class is not key value coding-compliant for the key enterButton

So I can know it is related to some IBOutlet named enterButton. Then you can know what is enterButton and what you have done to it.

What I learn is that, if you created the IBOutlet by control-dragging, and later you changed the name, sometimes you will break the mapping in xib file or storyboard.

If you search enterButton in xib file, you can see the xml file has defined some keys to get the correct UIView element to be initialized correctly. If you changed the name, the mapping is corrupted, so something goes south.

This is experience and when you are getting more and more familiar with iOS, you will know how to debug it. But I think, what you could do for now is to understand how the views and controllers are initialized from nib, and how to use LLDB to debug your code. This can always help.

Swift: Terminating with uncaught exception of type NSException is quite similar to your issues. Check it out.

like image 30
Wingzero Avatar answered Oct 11 '22 12:10

Wingzero