Key-value codingis a mechanism for accessing an object’s properties indirectly, using strings to identify properties, rather than through invocation of an accessor method or accessing them directly through instance variables. In essence, key-value coding defines the patterns and method signatures that your application’s accessor methods implement.
Listing 1 Implementation of data-source method without key-value coding
- (id)tableView:(NSTableView *)tableview |
objectValueForTableColumn:(id)column row:(NSInteger)row { |
|
ChildObject *child = [childrenArray objectAtIndex:row]; |
if ([[column identifier] isEqualToString:@"name"]) { |
return [child name]; |
} |
if ([[column identifier] isEqualToString:@"age"]) { |
return [child age]; |
} |
if ([[column identifier] isEqualToString:@"favoriteColor"]) { |
return [child favoriteColor]; |
} |
// And so on. |
} |
Listing 2 Implementation of data-source method with key-value coding
- (id)tableView:(NSTableView *)tableview |
objectValueForTableColumn:(id)column row:(NSInteger)row { |
|
ChildObject *child = [childrenArray objectAtIndex:row]; |
return [child valueForKey:[column identifier]]; |
} |