Guillaume Dubois
Guillaume Dubois

Reputation: 2003

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

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

My application is crashing with the following error"

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

What does this mean & How can I fix it?

Upvotes: 1332

Views: 1041014

Answers (30)

yoAlex5
yoAlex5

Reputation: 34341

iOS Run Time Error

Thread 1: "[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key xxx."

You can get this error when:

  1. .xib + UITableViewHeaderFooterView

When you work with UITableView where show header/footer which extends UITableViewHeaderFooterView and connect with .swift file and

  • set File's Owner

correct way:

  • set Custom Class for containerView instead
  1. Check Outlets in .xib(maybe after some refactoring). Some of them can be marked(or not) by Xcode using yellow explanation mark. In this case just resetup it

Upvotes: 5

flamyoad
flamyoad

Reputation: 565

In my case, I forgot to input the Module of my ViewController inside Custom Class section (located in the right hand side of the storyboard screen)

Upvotes: 1

Abhishek C. Gidde
Abhishek C. Gidde

Reputation: 169

In my case the View was UIView but the XIB Connection was to an IBOutlet of type LottieAnimationView. The actual lottie was a child of the UIView.

Upvotes: -1

younes alaoui
younes alaoui

Reputation: 133

As far as I'm concerned I stupidly changed the access modifier of my UIViewController to Private and of course it couldn't access it...

Upvotes: 0

mefahimrahman
mefahimrahman

Reputation: 349

This thread is really an epic saga as @FranticRock mentioned.

In my case -

  1. the outlets are properly connected
  2. the "Inherit From Target" has a proper value which means the module is properly added

What caused the crash in my case is User Defined Runtime Attributes under the Identity Inspector

I have a view that somehow had three attributes borderColor, borderWidth and cornerRadius

enter image description here

Removing these attributes from that view resolved the issue.

Upvotes: 0

Krishan Kumar
Krishan Kumar

Reputation: 399

enter image description here

Remove button reference by clicking the button and then re-create reference to this button.

Upvotes: 0

NhlanhlaNkosi
NhlanhlaNkosi

Reputation: 613

For me it, I had to click the file's Owner object and navigate to connections and removed the problematic connections

Upvotes: 1

yoAlex5
yoAlex5

Reputation: 34341

NSUnknownKeyException error

'NSUnknownKeyException', reason: this class is not key value coding-compliant for the key

I got this error when .xib file(e.g. TableViewCell.xib) had the same name as cell class(e.g. TableViewCell.swift)

The correct approach is:

TableViewCell.swift -> View.swift <-> View.xib

Upvotes: 0

Kyle Venn
Kyle Venn

Reputation: 8078

I tried literally every option in this thread. The only thing that fixed it for me was changing the name of my custom view xib file.

My custom view and xib had the same name. Renaming the xib fixed it.

Upvotes: 0

SaadurRehman
SaadurRehman

Reputation: 660

I am using the Xamarin.IOS to create the custom nib. Finally I found the root cause, went to Xcode and selected the nib file, In the Interface Builder, choose the UIView class and select the Connections Inspector and remove the default outlets as shown below.enter image description here

Upvotes: 0

Joakim Sj&#246;stedt
Joakim Sj&#246;stedt

Reputation: 948

In my case it was the module that was specified to the wrong target. Simply deleting the module name and checking Inherit Module from target did the trick.

enter image description here

Upvotes: 6

VoidMain
VoidMain

Reputation: 985

I also had this problem, it was due to renaming a view by creating a new outlet to it. Your might have the old connection outlet in the storyboard.

What you need to do is to remove the old outlet from the storyboard.

  1. Goto the storyboard.
  2. Click "Show Code Review" button (the one the <- -> sign on it, just left of the show/hide navigator).
  3. Search for the "connections" tag.
  4. Look for the "outlet" tag within the "connections" tag with the "property" attribute set to the name you are getting in the Exception.
  5. Remove that outlet tag.
  6. Hit "Command + B", Enjoy!

Upvotes: 2

FranticRock
FranticRock

Reputation: 3283

To add to this epic saga of a thread...

My view controller had an attribution above it: @objc(TheNameOfMyViewController)

This made all the Outlets crash with "not key value compliant" error for each Outlet. This was only an issue on IOS12 and below. It worked fine on iOS 13.

Removing that modifier fixed this problem. All the outlets are working fine now.

Upvotes: 1

TechZen
TechZen

Reputation: 64428

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

Upvotes: 1067

spaine
spaine

Reputation: 443

I encountered this error that occurred on all outlets in a custom class I had created for a table view prototype cell. The outlets were all correctly connected to the storyboard, and the Class field was correctly named in the identity inspector for the prototype cell.

Deleting and recreating the outlets did not work. Cleaning the build did not work. What finally worked was to change the Class in the storyboard identity inspector to the default UITableViewCell, hit Enter, then change it back to the name of my custom class afterwards. For some reason, this worked.

Upvotes: 1

nuttysimple
nuttysimple

Reputation: 2791

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

Upvotes: 1676

lee
lee

Reputation: 8115

Really stupid mistake with me. So I decided to share here, hope this will help another people like me.

I have another submodule in my project, and I name a new class with same name with another class in main module, my new class NOT have xib file(100% by code), but another class in main module have xib file. I think Xcode linked wrong the new class with the xib file in main module.

That the reason for crash with message:

this class is not key value coding-compliant for the key tableView.

Upvotes: 0

Abhi
Abhi

Reputation: 329

I got the same error when I manually loaded a view from a nib. It turns out that I had forgotten to set the view's owner.

For e.g. If you load a view in the following way,

 let view = Bundle.main.loadNibNamed("MyNibFileName",
                                 owner: nil,
                                 options: nil)?.first as! UIView

and then add an IBOutlet, the IBOutlet can't be referenced and the application will crash with the above error.

Fix: Assign an owner for the view.

 let view = Bundle.main.loadNibNamed("MyNibFileName",
                                 owner: self,
                                 options: nil)?.first as! UIView

Upvotes: 2

Bogdan Kobylynskyi
Bogdan Kobylynskyi

Reputation: 1220

I resolved this problem by doing 2 things:

  1. Fixed Class reference of the view:

  1. Reimported all Outlets:

Upvotes: 2

mrabins
mrabins

Reputation: 197

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

Upvotes: 164

Roman Reimche
Roman Reimche

Reputation: 143

In my case it was that an IBOutlet's property name in a ViewController.m was changed, but the one in Storyboard was not. Reintroducing the IBOutlet into the ViewController.m per ctrl-drag solved the problem. I have also noticed a way to find such "orphane" IBOutlets in XCode: (look at the image) the ones that are not "orphaned" have concentric circles instead of line numbers.

"Orphaned" IBOutlets don't have concentric circles in the line number column.

Upvotes: 2

Wes
Wes

Reputation: 1069

In my case, I had added a ViewController to the storyboard, but I didn't assign it a Storyboard ID in the designer. Once I gave it an ID, it worked.

using Xamarin/Visual Studio 2015.

Upvotes: 0

Suragch
Suragch

Reputation: 512506

Looking over the other answers it seems like there are a lot of things that can cause this error. Here is one more.

If you

  • have a custom view
  • added an @IBInspectable property
  • and then later deleted it

Then you may also get an error similar to

Failed to set (xxx) user defined inspected property on [Your Custom View] ...: this class is not key value coding-compliant for the key [xxx].

The solution is to delete the the old property.

enter image description here

Open the Identity inspector for your class, select the property name under User Defined Runtime Attributes, and press the minus button (-).

Upvotes: 23

2095377
2095377

Reputation: 47

It can come from the fact that you have control dragged and created an outlet or action, and forgot to delete it. Even if you deleted the code, or even if you have made enough cmd+Z, you'll need to go in the connection inspector of your storyboard and see if the action or outlet you created is still here or not.

Upvotes: 25

MBH
MBH

Reputation: 16639

My problem started after i changed the Target name.

My own custom UI classes had the Module name set to the old target name.

I changed the target name to the new one and it works fine now.

Upvotes: 1

user1760527
user1760527

Reputation: 1164

I had this problem with storyboard and swift class for ui view controller. Solved it by using the @objc directive:

@objc(MyViewController) class MyViewController

Upvotes: 2

Iulian Onofrei
Iulian Onofrei

Reputation: 9740

Make sure you add the custom class' (even empty) implementation in the .m file like:

@implementation MySubclass
@end

Upvotes: 0

Chris Allinson
Chris Allinson

Reputation: 1882

I had the same issue, and the cause was due to specifying a Module in Interface Builder (as opposed to leaving it blank). So, when I was using either a different module than the one I set, the app would crash :S ... hope this helps someone else, as my issue was not due to a broken or out-of-date outlet!

Upvotes: 1

Olle Raab
Olle Raab

Reputation: 96

The cause of my trouble, was that I duplicated a storyboard file (outside of Xcode if I recall correctly), then all view controllers in the duplicated file had same object-ID as in the original file. The remedy is to copy-pasted the view controllers, and they will then get a new object-ID. You can see the object-ID in the Identity Inspector.

Upvotes: 0

Ahmed Elashker
Ahmed Elashker

Reputation: 1000

Just pay attention if you're trying to observe a value that doesn't exist.

This happened to me simply as I was observing the "Text" property of a text field when I was supposed to be observing the "text" property instead.

Upvotes: 0

Related Questions