Xcode – How to fix ‘NSUnknownKeyException’, reason: … this class is not key value coding-compliant for the key X” error? [Answered]

Sample problem:

I’m trying to link a UILabel with an IBOutlet created in my class.

My application is crashing with the following error.

What does this mean?

How can I fix it?

*** Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[<UIViewController 0x6e36ae0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key XXX.’

Answer #1:

You may have a bad connection in your xib.

I’ve had this error many times. While TechZen’s answer is absolutely right in this case, another common cause is when you change the name of a IBOutlet property in your .h/.m which you’ve already connected up to File’s Owner in the nib.

From your nib:

  1. Select the object in IB and go to the ‘Connections Inspector’.
  2. Under ‘Referencing Outlets’ make sure that your object isn’t still connected to the old property name… if it is, click the small ‘x’ to delete the reference and build again.

Another common cause if you are using Storyboard, your UIButton might have more then one assignings (Solution is almost the same as for nib):

  1. Open your storyboard and right click the UIButton
  2. You will see that there is more than one assign/ref to this button. Remove one of the “Main…” greyed windows with the small “x”:

Answer #2:

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):

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

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

Answer #3:

Sometimes this has to do with your “Inherit From Target” That value has to be set. With single target apps you can just select Inherit From Target. If you have more then one target select the desired target.

enter image description here

Answer #4:

I had this error when I was trying to implement a custom ViewCell for a table. When I highlighted View controller for the XIB and connected to the elements in the CellView caused the error ” this class is not key value coding-compliant for the key” once I deleted these it got rid of the error.

Delete the connections in the below image. Delete the connections in inspector when File Owner is highlighted

Just make sure that you only have the connections with the Table View Cell. To check click on table view cell and in INSPECTOR look for your connections.

The connection should be in here when Table View Cell is highlighted

Answer #5: The simplest solution so far

I had to delete the app from the simulator/iPhone to get rid of this error.

Answer #6:

This error indicates that an already connected Interface Builder object is removed/renamed in its owner’s source (File’s Owner).

Control-click on the Files’s Owner in the Interface Builder if you see a exclamation mark you need to fix that.

In the picture below you can see that “aRemovedView” has an exclamation mark on its right, that’s because I removed the IBOutlet view object while it was already connected in the IB.

enter image description here

This gives the following error: Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key aRemovedView.’

Answer #7a:

I had the same problem and while TechZen’s answer may indeed be amazing I found it hard to apply to my situation.

Eventually I resolved the issue by linking the label via the Controller listed under Objects (highlighted in the image below) rather then via the File Owner.

Hope this helps.

enter image description here

Answer #7b:

In my case it was an error in the storyboard source code, follow these steps:

  1. first open your story board as source code
  2. search for <connections>
  3. remove unwanted connections

For example:

    <outlet property="mapPostsView" destination="4EV-NK-Bhn" id="ubM-Z6-mwl"/>
    <outlet property="mapView" destination="kx6-TV-oQg" id="4wY-jv-Ih6"/>
    <outlet property="sidebarButton" destination="6UH-BZ-60q" id="8Yz-5G-HpY"/>

As you see, these are connections between your code variables’ names and the storyboard layout xml tags.

Hope you learned something from this post.

Follow Programming Articles for more!

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at programming-articles.com, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →