May 2008
Intermediate to advanced
464 pages
8h 13m
English
Key-value coding (KVC) is a mechanism that allows you to set and get the value of a variable by its name. The name is simply a string, but we refer to that name as a key. So, for example, imagine that you have a class called Student that has an instance variable called firstName of type NSString:
@interface Student : NSObject
{
NSString *firstName;
}
...
@endsIf you had an instance of Student, you could set its firstName like this:
Student *s = [[Student alloc] init]; [s setValue:@"Larry" forKey:@"firstName"];
You could read the value of its firstName like this:
NSString *x = [s valueForKey:@"firstName"];
The methods setValue:forKey: and valueForKey: are defined in NSObject. Even though this doesn’t ...