Categories
cocoa cocoa-touch interface-builder ios macos

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

1286

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.’

11

  • 14

    pedrotorres is right. Yes this is right. If you are doing a UITableViewCell, in IB remember to make teh File’s Owner to NSObject, and the UITableViewCell’Class to the .h class you defined.

    – gdm

    May 24, 2013 at 7:56

  • 4

    When you encounter such an issue and the offending key is an action rather than an outlet then most probably you have an outlet which mistakenly references your action function name instead of your outlet variable name.

    Jun 4, 2015 at 15:50


  • 2

    You should notice that the name of the key in the error message (the OP is calling it ‘XXXX’) is the name you gave to something in your nib file. That’s should help narrow down your search.

    – kris

    Jul 12, 2015 at 21:51

  • I filed rdar://22105925 asking Apple to make these errors more obvious at build time. 🙂

    – jtbandes

    Aug 1, 2015 at 22:35

  • how to actually IMMEDIATELY SOLVE the problem! stackoverflow.com/a/13812660/294884 fantastic tip

    – Fattie

    Sep 28, 2015 at 13:01

1054

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

15

  • 10

    Also do not forget to connect the “view” outlet of the nib to the file owner’s view outlet (The view outlet of your custom class inherited from UIViewController). This can be done by control dragging from “File’s Owner” under “Place Holders” to “view” under “Objects” and selecting the view outlet.

    – Nirma

    Aug 11, 2011 at 14:49


  • 1

    I’ve made sure all the class are already set, but the NSUnknownKeyException still comes out 🙁

    Apr 3, 2013 at 3:40

  • 1

    I’m not even using Interface Builder, and I’m still getting this error. Any idea what’s going on?

    – sudo

    Mar 9, 2014 at 7:18

  • 14

    Never mind. It was because of two things: Xcode was still accessing my Main storyboard even though it was removed from the project, and the simulator had it cached. I highly unrecommend using storyboards or NIBs – they’re ridiculously problematic.

    – sudo

    Mar 9, 2014 at 7:24


  • 6

    @Josh Lol. Everytime I work up the motivation to create a native app in xcode it always ends with me spending a day trying to figure out how to do anything. I’m a c# dev so I’ve been looking at offerings from Xamarin and Telerik (nativescript) lately.

    May 13, 2015 at 15:02

1054

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

15

  • 10

    Also do not forget to connect the “view” outlet of the nib to the file owner’s view outlet (The view outlet of your custom class inherited from UIViewController). This can be done by control dragging from “File’s Owner” under “Place Holders” to “view” under “Objects” and selecting the view outlet.

    – Nirma

    Aug 11, 2011 at 14:49


  • 1

    I’ve made sure all the class are already set, but the NSUnknownKeyException still comes out 🙁

    Apr 3, 2013 at 3:40

  • 1

    I’m not even using Interface Builder, and I’m still getting this error. Any idea what’s going on?

    – sudo

    Mar 9, 2014 at 7:18

  • 14

    Never mind. It was because of two things: Xcode was still accessing my Main storyboard even though it was removed from the project, and the simulator had it cached. I highly unrecommend using storyboards or NIBs – they’re ridiculously problematic.

    – sudo

    Mar 9, 2014 at 7:24


  • 6

    @Josh Lol. Everytime I work up the motivation to create a native app in xcode it always ends with me spending a day trying to figure out how to do anything. I’m a c# dev so I’ve been looking at offerings from Xamarin and Telerik (nativescript) lately.

    May 13, 2015 at 15:02

140

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

8

  • This answer helped me for a specific case. I have to continu working on an old obj-c project with multiple target. I create a custom UITableViewCell in Swift and I had the same error. By enabling “Inherit From Target” it worked. Thank you!

    Apr 11, 2017 at 7:59

  • The module is not always set correctly; e.g. if you fill in the class name before actually creating the class.

    – Johan

    Dec 22, 2017 at 10:45

  • I have filled the class name without pressing “Enter” key at the end so the IDE didn’t checked this flag automatically. Wasted 1 hour on this. Thank you.

    – MatPag

    Aug 17, 2018 at 16:20


  • Never encountered this type of error in obj-c. But thanks its solved the mystery.

    Sep 13, 2019 at 6:57

  • why is this burried in the 3rd page?

    – Sergio

    Nov 14, 2019 at 17:56