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

Guillaume Dubois picture Guillaume Dubois · Jun 21, 2010 · Viewed 895.9k times · Source

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

nuttysimple picture nuttysimple · Nov 11, 2011

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.

    enter image description here

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

    example 2