Up: Concepts   [Contents][Index]


3 Key Value Coding

Key Value Coding is a concept used widely throughout GDL2, it provides a mechanism by where you can access and modify an objects set/accessor methods or even instance variables directly, through a named key.

Additionally some classes may implement KVC in a way specific to the class.

4 Setting values through KVC

Setting values through key value coding will try to call a method ’-setKeyName:’ with the value as the parameter to -setKeyName: as a parameter failing that, if anObject had an instance variable with the same name as the key that would be modified directly.

If anObject does not respond to ‘-setKeyName:‘ and there is no instance variable with the same name as the key, an exception is thrown.

[anObject setValue:@"bar" forKey:@"foo"];

Will first try to call -setFoo: then attempt to set the instance variable named "foo" to "bar".

5 Accessing values through KVC

Accessing values through Key Value Coding first attempts to call the -keyName method on anObject if it responds. If the object does not respond then it will try to access an instance v