By Guillaume Dubois


2010-06-21 19:57:32 8 Comments

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

30 comments

@TechZen 2010-06-21 20:29:07

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.

@Nirma 2011-08-11 14:49:13

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.

@Zennichimaro 2013-04-03 03:40:54

I've made sure all the class are already set, but the NSUnknownKeyException still comes out :(

@sudo 2014-03-09 07:18:12

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

@sudo 2014-03-09 07:24:17

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.

@Ilya Saunkin 2015-02-09 07:27:47

+1000 for what you say about bad naming practice. This is so obscure to figure out! I had a method called something like 'init....' and the application was crashing randomly all the time, then I renamed it and crashes just stopped.

@sabiland 2015-04-02 12:24:14

The problem in my case is sometimes -> User Defined Runtime Attributes. If you changed the class for control (and user defined attributes were meant for previous class), those attributes (non-existing for some other class) could throw just that exception.

@Josh 2015-05-13 14:31:35

@TheMuffinMan only 30 minutes? I have already lost one day of work because of this nonsense!

@The Muffin Man 2015-05-13 15:02:24

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

@TechZen 2015-05-14 14:38:22

Swift Language solves a lot of these problems by nailing everything down at compile time, but you lose the flexibility of Objective-C runtime.

@Jacob King 2015-07-17 14:53:18

Turns out I was inheriting a subclass that already had an outlet for the object, meaning that it had essentially 2 outlets pointing to the same place, causing the crash.

@deepax11 2016-05-19 10:53:42

I had the same issue but fixed it in different way. My project has different build configuration (pointing to different environment) which creates build with different bundle identifier. Somehow one of my cell's module (in Interface builder) was set to specific bundle identifier. I just set it to current-module to make it work. Setting none did not work.

@Maury Markowitz 2017-01-04 14:12:41

I would also like to point out a "bug" in Xcode that can insert these errors for you. If you make a connection by control-dragging into the class, and then notice that you made an outlet instead of an action (or vice versa), when you delete the code it leaves the connection behind. You have to manually disconnect it in the xib/sb editor.

@MoolsBytheway 2018-01-11 01:28:37

quoting: 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. <= that was it for me

@qwertzguy 2018-10-07 01:14:01

Make sure you select 'Inherit Module From Target' when using a custom view controller written in swift.

@mohammad alabid 2019-01-14 22:12:35

happened me duo to non ticked target

@mrabins 2017-04-05 18:51:03

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

@magohamoth 2017-04-11 07:59:06

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!

@Johan 2017-12-22 10:45:38

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

@MatPag 2018-08-17 16:20:04

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.

@Vinoth Vino 2018-12-11 13:06:09

Thanks it worked for me...

@Roman Reimche 2018-06-18 05:06:40

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.

@Wes 2018-02-16 17:38:21

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.

@Suragch 2015-08-22 08:06:21

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 (-).

@Ilansky Naftali 2017-08-12 12:08:19

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.

@kunigami 2018-01-22 07:00:32

Thanks! I'm a n00b on Xcode and took me a while to find the rogue links. In Xcode 9.2, you can right click on the element in the storyboard view and check the "Referencing outlets".

@MBH 2017-07-19 05:24:31

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.

@user1760527 2017-04-04 12:08:55

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

@objc(MyViewController) class MyViewController

@Iulian Onofrei 2017-03-13 15:41:56

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

@implementation MySubclass
@end

@Chris Allinson 2017-02-06 22:57:15

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!

@Olle Raab 2016-11-30 22:30:27

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.

@nuttysimple 2011-11-10 23:14:29

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.

    example 1

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

@Mark Patterson 2014-02-03 22:31:43

Thanks! In my case I had 2 outlets to the same view, and getting rid of the old one and hooking up the view to the new one ended the exception.

@aremvee 2016-01-26 04:16:39

mine was duplicated twice. I began disconnecting each one after i dragged and dropped from a Pro version ( from a Lite version ) thinking it was hanging onto connections in the other project. When i came across the duplicate, and it took 3 clicks to make the disconnection, i sincerely wished i done that 3 days earlier. Oh well- such is the life of a dev.

@mythicalcoder 2016-04-17 16:07:43

I had the problem that UIButton was having more than one assignings. This solved my issue.

@Augusto Carmo 2016-09-19 14:29:32

I had this problem too. Don't forget to check outlet connections in particular views instead of only superviews. In my case, superviews weren't showing the wrong connections =/

@brw59 2017-02-24 23:56:42

I had a similar error where I connected a button outlet to a the wrong ViewController swift file whose page would not have been created yet using the GUI

@Vince O'Sullivan 2018-04-22 13:13:21

What are the two icons (square spiral, resizing rectangle) to the right of the blue "Connections Inspector" (arrow in a circle) icon in the above answer. I note that the answer is from 2016 and that I'm asking this in 2018 but I don't recall ever seeing those icons before.

@ram 2018-06-12 12:20:05

I got the same error In swift 4, xcode 9. I had copied a UILabel from one controller (cmd-c) and pasted it into another (cmd-v). Later I deleted the label from the second controller, but the outlet somehow didn't get removed. After deleting it (as shown in this answer), the error got resolved.

@Daniel Beltrami 2018-09-13 19:41:54

Have the same error over and over again, on diferents ways, even when I take the greatest care possible! I think is a bug on Xcode, is not possible that I messed up this ever time.

@Mak083 2018-09-26 10:05:34

In my case, that was added twice with different name. (y)

@Shruti Thombre 2018-10-10 05:15:39

I had the same problem..above solution work for me

@Ahmed Elashker 2016-10-13 23:52:30

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.

@Emy Stats 2016-04-08 12:45:30

"this class is not key value coding-compliant for the key" I know its a bit late but my answer is different so I thinks it needs to be posted, I was pushing the second controller in wrong way, Here is sample

Wrong Way to Push Controller

UIViewController* controller = [[UIViewController
 alloc]initWithNibName:@"TempViewController" bundle:nil];
         [self.navigationController pushViewController:controller animated:true];

Correct way

TempViewController* controller = [[TempViewController
 alloc]initWithNibName:@"TempViewController" bundle:nil];
         [self.navigationController pushViewController:controller animated:true];

I did not found any answer like above so may be it can help some one having same issue

@tymac 2016-08-08 08:29:26

Did you leave your picker object unconnected?

lol 54 answers and not one of them is my solution. Talk about a common error.

In my case it was because I had a picker object on my VC and had not set an outlet and action for it.

I often leave buttons etc unconnected when I am just looking to see how the layout looks. But it seems you cannot do this for a picker.

@Damo 2016-07-07 13:38:21

If you have a custom UIViewController subclass with IBOutlets that are causing problems the only set of steps I found to actually get rid of the error were

.1 Change the class to UIViewController

.2 Disconnect all the outlets (they will all have the yellow warning triangle now) - it may be enough just to disconnect the problematic outlet(s).

.3 Do all the standard steps - ↑⌘K, delete Derived Data (, prayer mats, worry beads)

.4 Launch the app - go to the problematic scene.

.5 Kill the app, go back to Interface Builder change the class back to your custom class name.

.6 Reconnect your outlets.

Launch the app & this will normally have cleared up the key-value compliance issues.

@IsPha 2016-07-03 20:10:42

You may need to delete the outlet , recreate it by drawing form IB to .H file.

@CommaToast 2016-06-20 21:50:57

It could mean you're trying to use a sort descriptor without an atsign at the beginning. As in:

[array valueForKeyPath:@"distinctUnionOfObjects.self"]

Which must actually be:

[array valueForKeyPath:@"@distinctUnionOfObjects.self"]

@IanS 2016-06-20 13:08:43

I ran into the same problem for a different reason: I was using the main storyboard as the launch screen file. I guess if you are using a storyboard as the launch screen file, it shouldn't be connected to the view controller as it won't have been loaded yet.

@IanS 2016-06-20 13:04:21

I had the same issue when I was using the main storyboard as the launch screen file. I guess if you are using a storyboard as the launch screen file, it shouldn't be connected to the view controller as it won't have been loaded yet.

@Fonix 2016-05-17 06:15:53

I had this happen when i had a UIView linked into a storyboard and connected IBOutlet like:

@property (strong, nonatomic) IBOutlet UIView *someView

but later on, i made the UIView into a custom class but forgot to change the class name of this IBOutlet defined above. its a strange error because if the custom view has no subviews it doesnt complain, but as soon as there are subviews it shows the error stated in the question but on one of the subviews and not the outer UIView where the problem actually exists. had me deleting and readding all the subviews trying to find the problem, when in fact had nothing to do with the subviews

@Fareed Alnamrouti 2014-07-22 22:11:53

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:

<connections>
    <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"/>
</connections>

As you see, these are connections between your code variables' names and the storyboard layout xml tags ;)

@Navneet Krishna 2017-11-30 06:04:39

worked for me :)

@Honey 2016-03-31 18:49:28

Along with other issues that you can see in other answers. The way I created this error for myself was that I started a project from scratch and begin by deleting the initial scene of my storyboard and then pasted a scene from another project into my storyboard.

There is no problem in doing that. You only need to add an entry point of your storyboard i.e. check the is initial View Controller on which ever view controller you like. Otherwise it will be a gray scene and throw you an error.

enter image description here

@Avinash Jadhav 2016-02-26 16:17:33

You may have outlets to UI Element but not IBOutlet property in .h File

For all UI element in Connection Attribute check outlets and it corresponding property in .h header file.

May be missing one or more property entry in .h file.

@Jyotsna 2016-02-23 05:25:55

I had the same issue.This happened with my project because I changed my product name but in interface builder I had the old name as Module, this leads to crash so be sure to check all the xib module name also has been changed or not.

@AtheistP3ace 2016-01-26 15:24:03

Just to add to this, because I was getting this error as well. Going through all these answers most seem to apply to working with the UI and storyboard stuff. I know the original poster did seem to be working with the UI but when searching for possible reasons for this error mostly all questions lead back to this question with those others being closed as duplicates or simply having issues with connecting things in a storyboard so I will add my solution.

I was working on coding a web service in Swift 2. I had built all the needed proxy objects and stubs. As I looped through the returned XML I was dynamically instantiating my objects, which all came from NSObject and using setValue:forKey on them. Every time setValue:forKey tried to set a property it blew up with this error.

I had a switch statement for each type I was dealing with (e.g. Bool?, CShort?, String?) and for each XML node I went through and checked what the type was on the object and then converted the value to that type and attempted to set it with setValue:forKey.

Eventually I started commenting out all these setValue:forKey lines and found that my default switch statement case did work for String?.

I finally figured out that you can't use optional swift types with setValue:forKey unless they have a direct mapping to an Objective-C type like String? or NSNumber?. I ended up changing all CShort? types to NSNumber? since that has a direct mapping. For Bool? in my case it was fine for me to just use Bool and initialize it to false. Others may not have that luxury.

Anyway what a headache that was so hopefully this helps someone else who has a similar problem and keeps getting redirected to this question and saying to themselves, "I am not doing anything in UI!!".

So lastly to reiterate one more time Key-Value Coding does not work with optionals. Below I ended up finding somewhere but I forget where so to whoever posted this I apologize and would give credit if I remembered where I found this but it saved my life:

You cannot use KVC on an Optional Int property, because KVC is Cocoa / Objective-C, and Objective-C cannot see an Optional Int - it is not bridged to Objective-C. Objective-C can only see types that are bridged to Objective-C:

class types that are derived from NSObject

class types that are exposed with @objc

Swift structs that are bridged

@Malder 2015-10-02 09:12:57

In my case. I didn't have missing outlets in xib-files after merging.

Shift + Command + K

solved my problem. I cleaned my project and rebuilt.

@user1760527 2015-09-27 05:47:05

I had this problem with swift classes after upgrading to xcode7. Solved it by using the @objc directive:

@objc(XXXViewController) class XXXViewController

@Vladimir Borodko 2015-09-24 16:56:25

Another tricky case:

In IB added ViewController to storyboard, deleted its view and set Custom Class to "MyViewController" so view is instantiated from MyViewController.xib

Specifying Storyboard ID the same "MyViewController" causes exception.

Changing Storyboard ID to some different name resolves the issue.

@ercu 2015-09-20 16:24:14

"Module" property of View Controller in the Identity Inspector might be different than what you expected. Also make sure that new classes are added to your target list.

@Mixaz 2016-04-26 09:58:11

voting up since it was so in my case. I'm using Swift and Storyboards. When I created a new build target (to make a build variant) the app didn't work in the new variant while the first one worked OK. The app crashed on a View Controller where the module name was somehow set to module from the first target, while in working View Controllers it was Current - <module name here>. You need to erase the module name there in this case.

@joliejuly 2018-07-04 18:10:08

You just saved my life! Thank you so much!!!

Related Questions

Sponsored Content

27 Answered Questions

[SOLVED] Loaded nib but the 'view' outlet was not set

1 Answered Questions

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

  • 2017-05-16 13:29:09
  • Krunal Patel
  • 174 View
  • 0 Score
  • 1 Answer
  • Tags:   ios

10 Answered Questions

[SOLVED] What does the NS prefix mean?

3 Answered Questions

[SOLVED] "This class is not key value coding-compliant for the key" error

20 Answered Questions

1 Answered Questions

4 Answered Questions

[SOLVED] This class is not key value coding-compliant for the key...why?

5 Answered Questions

[SOLVED] Uncaught exception: This class is not key value coding-compliant

1 Answered Questions

Sponsored Content