How to Catch Nsunknownkeyexception in Swift 2.2

Why do I get NSUnknownKeyException when I click on a button?

It seems that you have no outlet defined (anymore). The storyboard still thinks there is an @IBOutlet weak var button_1: UIButton? but you have deleted it apparently.

Swift NSUnknownKeyException

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.

Sample Image

Sample Image

Terminating app due to uncaught exception 'NSUnknownKeyException', What's wrong?

This part of the crash log:

2020-12-01 12:42:41.206345-0800 final_proj690[3252:142523]
[Storyboard] Unknown class HiraganaViewController in Interface Builder
file.

2020-12-01 12:42:41.224722-0800 final_proj690[3252:142523] ***
Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<UIViewController 0x7fa939d12790>
setValue:forUndefinedKey:]: this class is not key value
coding-compliant for the key Question.'

Shows 2 different problems:

Problem 1:

It says you reference a class HiraganaViewController which you don't define.

Yet I see a class definition for that class in the source code you posted. Open the "HiraganaViewController.swift" file in Xcode, and then select the "file inspector". Make sure that there is a checked entry in your "target membership" section that includes that source file into your application.

Problem 2:

You have an IBOutlet named Question. It looks as though that view is not connected to its outlet. That may be caused by the first error. If you fix the first error and this still occurs:

Go into the storyboard and select the HiraganaViewController in its scene. Then select the connection inspector, look for an entry for Question, and click the X on it to break the link to the IBOutlet. Then open a second assitant editor and control-drag from the Question object to the IBOutlet declaration.

Swift: App terminating due to uncaught exception 'NSUnknownKeyException'

The problem was that the button was linked to numerous IBActions - three of which didn't exist. I right clicked on the button in the Interface Builder and removed the old and irrelevant links, fixing the problem.

Xcode - How to fix 'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X error?

Your view controller may have the wrong class in your xib.

I downloaded your project.

The error you are getting is

'NSUnknownKeyException', reason: '[<UIViewController 0x3927310> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key string.'

It is caused by the Second view controller in MainWindow.xib having a class of UIViewController instead of SecondView. Changing to the correct class resolves the problem.

By the way, it is bad practice to have names like "string" in Objective-C. It invites a runtime naming collision. Avoid them even in once off practice apps. Naming collisions can be very hard to track down and you don't want to waste the time.

Another possible reason for this error: when copying & pasting elements from one controller into another, Xcode somehow keeps that link to the original controller, even after editing & relinking this element into the new controller.

Another possible reason for this error:

Bad Outlet.

You have either removed or renamed an outlet name in your .h file.

Remove it in .xib or .storyboard file's Connection Inspector.

One more possible reason

(In my case) Extension of UIView with bindable properties and setting values for those bindable properties (i.e. shadow, corner radius etc.) then remove those properties from UIView extension (for some reason) but the following <userDefinedRuntimeAttributes> remained in xml (of foo.storyboard):

<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="color" keyPath="shadowColor">
<color key="value" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="shadowOpacity">
<real key="value" value="50"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="point" keyPath="shadowOffset">
<point key="value" x="5" y="5"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="shadowRadius">
<real key="value" value="16"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="borderWidthValue">
<real key="value" value="0.0"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>

Solution: Right click on foo.storyboard > Open as Source Code > search by keyPath (i.e. shadowRadius) > Delete the </userDefinedRuntimeAttributes> that causing the problem

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

"this class is not key value coding-compliant for the key pause" usually means you have a referencing outlet problem. Look in the Connections Inspector for your different buttons. You may likely either have:

2 referencing outlets for one button and the program does not know which ne to use, etc.

I ran the code hooking up 1 label to an IBOutlet and three buttons (play, pause, reset), one to each IBAction, and it ran perfectly.



Related Topics



Leave a reply



Submit