Skip to Main Content
Cocoa Programming for Mac OS X, Third Edition
book

Cocoa Programming for Mac OS X, Third Edition

by Aaron Hillegass
May 2008
Intermediate to advanced content levelIntermediate to advanced
464 pages
8h 13m
English
Addison-Wesley Professional
Content preview from Cocoa Programming for Mac OS X, Third Edition

Chapter 7. Key-Value Coding; Key-Value Observing

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;
}
...
@ends

If 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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

COCOA PROGRAMMING FOR MAC OS X SECOND EDITION

COCOA PROGRAMMING FOR MAC OS X SECOND EDITION

Aaron Hillegass

Publisher Resources

ISBN: 9780321562739Purchase book