Key Value Coding 4: Using A Dictionary Instead Of ivars
2006-08-22
Download and open up KVO4. Here is KVO4 running:

This Key Value Coding example replaces the individual ivars of KVO3 with a dictionary and adds a third car. It also replaces the Car class with a Vehicle class. AppController now contains an ivar called cars that is an NSMutableDictionary containing Vehicles:

The Vehicle class has no accessors.
AppController reads the values out by using valueForKey twice: once to access the dictionary to find the Vehicle, and then again to access the ivar inside the Vehicle object:

And since the Vehicle class handles the unknown key case by returning an "Unknown: " string:

we get the same kind of result as we did for the Car class.

This Key Value Coding example replaces the individual ivars of KVO3 with a dictionary and adds a third car. It also replaces the Car class with a Vehicle class. AppController now contains an ivar called cars that is an NSMutableDictionary containing Vehicles:

The Vehicle class has no accessors.
AppController reads the values out by using valueForKey twice: once to access the dictionary to find the Vehicle, and then again to access the ivar inside the Vehicle object:

And since the Vehicle class handles the unknown key case by returning an "Unknown: " string:

we get the same kind of result as we did for the Car class.
The Bagelturf site welcomes Donations of any size