Why does my object not meet key encoding requirements?

Trying to use key encoding to set the value for 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 the engine key value corresponding to the encoding?

+1
source share
2 answers

Ensure that the engine class belongs to the horsePower property horsePower and / or has an instance variable horsePower and / or manually implements the setHorsePower: and horsePower .

You get an error because you are trying to set the value of the horsePower key, but the engine class horsePower not correctly implement the way to set the horsePower key.

+4
source

There are quite a number of approaches to using KVC - perhaps the simplest is simply to have an instance variable in your class using the name of the key you want to use, i. e :.

 @interface Engine: NSObject { NSNumber *horsePower; // etc. } 
+3
source

Source: https://habr.com/ru/post/1334443/


All Articles