19

I'm writing my first iPhone app, so I haven't gotten around to figuring out much in the way of debugging. Essentially my app displays an image and when touched plays a short sound. When compiling and building the project in XCode, everything builds successfully, but when the app is run in the iPhone simulator, it crashes.

I get the following error:

Application Specific Information:
iPhone Simulator 1.0 (70), iPhone OS 2.0 (5A331)
*** Terminating app due to uncaught exception 'NSUnknownKeyException', 
reason: '[<UIView 0x34efd0> setValue:forUndefinedKey:]: this class is not key value 
coding-compliant for the key kramerImage.'

kramerImage here is the image I'm using for the background.

I'm not sure what NSUnknownKeyException means or why the class is not key value coding-compliant for the key.

jtbandes
  • 115,675
  • 35
  • 233
  • 266
btw
  • 7,006
  • 9
  • 40
  • 40

8 Answers8

22

(This isn't really iPhone specific - the same thing will happen in regular Cocoa).

NSUnknownKeyException is a common error when using Key-Value Coding to access a key that the object doesn't have.

The properties of most Cocoa objects can be accessing directly:

[@"hello world" length]    // Objective-C 1.0
@"hello world".length      // Objective-C 2.0

Or via Key-Value Coding:

[@"hello world" valueForKey:@"length"]

I would get an NSUnknownKeyException if I used the following line:

[@"hello world" valueForKey:@"purpleMonkeyDishwasher"]

because NSString does not have a property (key) called 'purpleMonkeyDishwasher'.

Something in your code is trying to set a value for the key 'kramerImage' on an UIView, which (apparently) doesn't support that key. If you're using Interface Builder, it might be something in your nib.

Find where 'kramerImage' is being used, and try to track it down from there.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
amrox
  • 6,207
  • 3
  • 36
  • 57
  • 2
    I'm getting this error, and it is something to do with interfacebuilder, but I cannot figure how to fix it. – bentford Sep 24 '08 at 23:58
  • 1
    Thanks for that - another wasted 3 hours finally fixed. :) – Toby Allen Aug 03 '10 at 16:40
  • I was having this problem in the situation where I'd copied files from one project to another. My xib referred to the wrong class name. This answer gave me enough info for a eureka moment, so thanks. – Grumdrig Aug 24 '10 at 21:37
  • Thanks for the good explanation. It doesn't help me a lot though as I am still confused.. I am just loading a bundle for TableCellView, but everything crashed even before anything: http://pastebin.com/QuR29q6B – Zennichimaro Mar 26 '13 at 08:04
10

Also when you rename a view, don't forget to delete the reference on File's Owner. It may also raise this error.

Ozkan Altuner
  • 101
  • 1
  • 4
8

Here's one where you'll get this error - and how to fix it. I was getting it when loading a nib that just had a custom TableViewCell. I used IB to build a xib that just had the File's Owner, First Responder, and the TableViewCell. The TableViewCell just had 4 UILabels which matched up to a class with 4 IBOutlet UILabels called rootCell. I changed the class of the TableViewCell to be rootCell. It worked fine until a made a couple of changes and suddenly I was getting the setValue:forUndefinedKey: when I was just instantiating the class after loading it from a nib:

    NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"rootCell-iPad" owner:self options:nil];

    cell = [nib objectAtIndex:0];

It failed at the first line, when the nib was trying to load. After a while, I noticed that it was trying to match the IBOutlet Labels to the root controller, NOT the rootCell class! That was my tipoff. What I did wrong was to inadvertently change the File's Owner to rootCell class. When I changed it back to NSObject, then it didn't try to match up to the delegate (rootController) when loading. So, if you're doing the above, make File's Owner an NSObject, but make the UITableCell your desired class.

Owen Hartnett
  • 5,925
  • 2
  • 19
  • 35
  • +1 And note to myself and anyone else who _always_ forgets this: when using loadNibNamed:owner:options:, the File's Owner should be NSObject, the main view should be your class type, and all outlets should be hooked up to the view, _not_ the File's Owner. – Echelon May 31 '11 at 19:52
  • Thanks Owen, I struggled with exactly the same situation. Resolved as described. Thanks a lot! – Khiet Apr 10 '12 at 22:25
2

I had this situation and it turns out even after finding all instances of the variable and deleting them my app still crashed. Heres what happened... I created another instance of a variable from a textfield from my XIB into my viewController.h but I realized I didnt need it anymore and deleted it. It turns out my program saw that and kept trying to use it in the program so in the future if this happens to anywhere else just go into you XIB right-click on the button or textfield etc and delete any extra unused variables.

Leon
  • 5,701
  • 3
  • 38
  • 38
2

This is how i solved mine, in Interface builder right-click the View Controller, there should be an exclamation mark on the missing outlet or action method. Find and remove all the references and that solved it.

This happened because i deleted the action method in the .m file.

Joseph
  • 5,793
  • 4
  • 34
  • 41
2

I had this same problem today. I didn't have the right class specified for the View Controller in my Navigation Controller. This will happen often if you fail to specify the correct class for your views in Interface Builder.

You'll also get invalid selector issues as well. Always check your Interface Builder classes and connections!

user229044
  • 232,980
  • 40
  • 330
  • 338
gonzobrains
  • 7,856
  • 14
  • 81
  • 132
  • Thanks. I had the class specified correctly in IB, but there was a mismatch between my controller and the nib I specified in initWithNibName: - I should have my copy+paste privileges revoked. – masty Sep 28 '12 at 04:32
  • Just go back to the iOS dojo and practice your copy+paste-fu. – gonzobrains Sep 29 '12 at 05:16
0

If you have done this code somewhere else and had a zip/compressed file, try to extract it again. It may work I dont know why but I feel like it is an extraction issue.

or you can try to change the IBOutlet to kramerImage and bind it again in NIB.

KETAN
  • 481
  • 1
  • 6
  • 14
0

It seems you are doing

@interface MyFirstIphoneAppViewController : UIViewController<> {
    UIImageView *InitialkramerImage;
}
@property(nonatomic,retain) IBOutlet UIImageView *InitialkramerImage;

Then after synthesizing that imageview, When you open "MyFirstIphoneAppViewController.xib" in Interface Builder, you are taking an Image View from Tool(menu)/Library den linking that outlet to the 'InitialkramerImage' from the Files Owner of "MyFirstIphoneAppViewController.xib". Then you saved the project. But after that you might change the name of the outlet variable "InitialkramerImage" to "kramerImage". So, after doing this

@interface MyFirstIphoneAppViewController : UIViewController<> {
    UIImageView *kramerImage;
}
@property(nonatomic,retain) IBOutlet UIImageView *kramerImage;

and saving the project when you run it, There is no existance of the outlet of "InitialkramerImage" in the "MyFirstIphoneAppViewController.xib". So, when you run the project there will be no outlet referencing from Imageview to the 'kramerImage' and

"For displaying the view, UIViewController will try to find the outlet to "InitialkramerImage" which is not existing."

So, It will throw the "NSUnknownKeyException".

You can check the missing outlet by opening the nib(.xib) file then right clicking on the 'Files Owner' of that.

Vipul Parmar
  • 104
  • 1
  • 4