1

Trying to use key-value coding to set a value on my object:

[engine setValue:[NSNumber numberWithInt:horsePower]
          forKey:@"horsePower"];

Causes an error:

[<Slant6 0x7fbc61c15c40> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key horsePower.

What do I need to do to make engine key value coding-compliant?

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29
  • Now where did I leave my crystal ball... – dreamlax Jul 15 '12 at 12:39
  • Unless you tell us the class of your engine object, there's not too much we can do to help you. – Thorsten Karrer Jul 15 '12 at 12:41
  • @ThorstenKarrer although in general it's a good kind of advice for novices to publish their actual code, here it is *not needed*. –  Jul 15 '12 at 12:44
  • @H2CO3 depends... if the OP was, for example, trying to call setValue:forKey: on an object of class NSDictionary, a better solution than to add an ivar would have been to change the class to NSMutableDictionary. Also, in this case, the question would have been a duplicate. – Thorsten Karrer Jul 15 '12 at 12:47
  • @H2CO3: There could be a number of issues though. `engine` may in fact be KVC-compliant for `horsePower`, but memory management issues may be causing the lookup to be done on the wrong object, etc. – dreamlax Jul 15 '12 at 12:48
  • @dreamlax but in this case we're already talking about another (irrelevant) class. –  Jul 15 '12 at 12:51
  • @H2CO3: What do you mean? There are no classes mentioned in the entire question, only an instance of something called `engine`. – dreamlax Jul 15 '12 at 12:53
  • #import @interface Engine : NSObject @end//Engine – Lucas Huang Jul 15 '12 at 12:54
  • #import "Engine.h" @implementation Engine -(NSString *)description { return (@"I am an engine.Vrooom!"); }//description @end//Engine – Lucas Huang Jul 15 '12 at 12:54
  • @ThorstenKarrer that's true, but 1. I was kind enough to assume OP knew the awesome site at http://208.117.229.184 2. you're right in the latter, because if it really was a dictionary, adding an ivar is not advisable as it can only be done by subclassing which is discouraged according to Apple. –  Jul 15 '12 at 12:54
  • thanks for all the people who answer for this question – Lucas Huang Jul 15 '12 at 13:01

2 Answers2

4

Make sure whatever class engine belongs to implements a horsePower property, and/or has a horsePower instance variable, and/or manually implements setHorsePower: and horsePower methods.

You are getting the error because you're trying to set the value of the horsePower key but engine's class does not properly implement a way to set the horsePower key.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
3

There are quite a bunch of approaches of taking advantage of KVC - perhaps the simplest is to just have an instance variable in your class with the name of the key you want to use, i. e.:

@interface Engine: NSObject {
    NSNumber *horsePower;
    // etc.
}
  • @FOCUSHUANG no problem, I'm happy to help. Next time please make sure you post some code, it greatly helps others to answer your question. –  Jul 15 '12 at 12:59
  • You should probably link to the KVC docs which explain all of the ways to make your class KVC-compliant. – Richard J. Ross III Jul 15 '12 at 13:28
  • Link to Apple KVC docs: [link](http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/KeyValueCoding/Articles/KeyValueCoding.html) – Jalakoo May 05 '13 at 00:16