views:

5886

answers:

3

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.

+10  A: 

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

amrox
I'm getting this error, and it is something to do with interfacebuilder, but I cannot figure how to fix it.
bentford
Thanks for that - another wasted 3 hours finally fixed. :)
Toby Allen
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
+1  A: 

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

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