iOS Development by David Casserly

From the Blog

Doing some iPhone development and this error really caught me out for a while:

2009-11-16 21:02:48.387 Pickers[3779:207] *** Terminating app due to uncaught 
exception 'NSUnknownKeyException', reason: '[<UIViewController 0x381d740> 
setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key datePicker.'
2009-11-16 21:02:48.388 Pickers[3779:207] Stack: (

It’s seemed fairly cryptic at the time. I had created a Tab Bar Controller and for each tab button, i hadn’t set its class identity – so each tab was still declared as a UIViewController instead of the actual Controller class that i had created.

The error comes from the NSKeyValueCoding category which allows you to access properties via setValue: forKey:

Person *aPerson = [[Person alloc] initWithAge: 53];
aPerson.name = @"Steve"; 
// NOTE: dot notation, uses synthesized setter, equivalent to [aPerson setName: @"Steve"];
NSLog(@"Access by message (%@), dot notation(%@), 
    property name(%@) and direct instance variable access (%@)",
      [aPerson name], aPerson.name, [aPerson valueForKey:@"name"], aPerson->name);

So i guess it is just complaining that it can’t find the generated setter method named setDatePicker – because datePicker instance is not on the generic class.