Wiki source code of Overview-Key Value Coding

Last modified by Pascal Robert on 2007/09/03 13:25

Hide last authors
Pascal Robert 6.1 1 Key-value coding is a way to separate interfaces and a way to flexibly access data and methods in object instances. The concept has a long history. It appeared in [[Eiffel>>url:http://en.wikipedia.org/wiki/Eiffel_(programming_language)||shape="rect"]], and other places as well. If you are familiar with [[OGNL>>url:http://en.wikipedia.org/wiki/OGNL||shape="rect"]] then you will know it from there. We'll call key-value coding KVC in the context of this page to abbreviate. KVC also exists in the Cocoa infrastructure shipped with MacOS-X, having evolved from OPENSTEP and the development tools created for it. In this page we will focus on KVC in the context of WebObjects and java.
smmccraw 1.1 2
Ray Kiddy 3.1 3 = Conceptual Aspects =
smmccraw 1.1 4
ianjoyner 4.1 5 Consider the following model segment:
smmccraw 1.1 6
Pascal Robert 6.1 7 [[image:attach:ModelForKVCArticle.png]]
smmccraw 1.1 8
Pascal Robert 6.1 9 This model shows a number of entities and the lines between the entities show relationships. For example, the Organisation entity has a to-one relationship with a Worker called "workerAccount" and it also has a "to-many" relationship to "OrgRegion" called "orgRegions".
smmccraw 1.1 10
Pascal Robert 6.1 11 The term "instance" here is referring to a specific organisation. For example, "The Corn Flour Company" and "Aqua Breweries" might be instances of the Organisation entity.
smmccraw 1.1 12
Ray Kiddy 3.1 13 == Value For Key ==
smmccraw 1.1 14
Pascal Robert 6.1 15 An instance of an Organisation can supply values for the keys "address", "countryIso", "email" etc... and they will return a value for the key. It is essentially acting like an NSDictionary or a Map object. This is how data is extracted from an object.
smmccraw 1.1 16
Ray Kiddy 3.1 17 == Take Value For Key ==
smmccraw 1.1 18
Pascal Robert 6.1 19 This is where an instance of an entity is asked to "take up" some value for a key. For example, an instance of an organisation might be told to take the value "foo@foo.co.nz" for the key "email". This is how data is loaded into an object using KVC.
smmccraw 1.1 20
Ray Kiddy 3.1 21 == Value For Key Path ==
smmccraw 1.1 22
Pascal Robert 6.1 23 Using a dotted path, it is possible to traverse relationships to access data in other entities. For example, if you have an instance of "OrgExpertise", it is possible to get the value for the key path "organisation.workerAccount.name". This is how you can extract data from the interconnected network of object instances.
smmccraw 1.1 24
Ray Kiddy 3.1 25 == Take Value For Key Path ==
smmccraw 1.1 26
Pascal Robert 6.1 27 Using a dotted path, it is possible to get an object in the interconnected network of object instances to "take up" some piece of data. For example, if you have an instance of "OrgExpertise" then it is possible to get the object to "take up" a value of "Joe Smith" for the key path "organisation.workerAccount.name".