Hi,
I have been confused myself by the KVC, KVO and I still not quite familiar with it.
I think the main gain of the KVC is the generic way it works.
Lets say that you have a call will several instance variables. I will use two of them since it is easier to write the example.
Code:
NSString *manufacturer;
NSString *regPlate;
Then you create setters and getters, I show only setters:
Code:
- (void) setManufacturer: (NSString *)val;
- (void) setRegPlate: (NSString *)val;
Now you created an instance of that class and call it ourCar.
You can set the data in the normal way
Code:
[ourCar setManufacturer:@”Volvo”];
[ourCar setRegPlate:@”XYZ 123"];
Or using KVC
Code:
[ourCar setValue @”Volvo” forKey @”manufacturer”];
[ourCar setValue @”XYZ 123” forKey @”regPlate”];
It seems the same and in the KVC example you even have more to write. To be more funny the KVC is using your setters…
Now imagine that you have to make a real program. Then the names can change and it is not fun to make changes all over the code manually.
Lets say that you keep the cars in a array, a data base or something similar.
You use loop to set them and now you will use the KVC to make it simple and generic. You really need only one line to do the set job.
Code:
[carList setValue value forKey key];
Is it not simpler ???
You just have to supply the keys and values and you have a generic pice of code.
There are as well possibilities to set all the instance values of an object in a single line by using
setValuesForKeysWithDictionary but it is another story. Good luck with the book.
Hope it will help
/petron