O'Reilly logo

Cocoa in a Nutshell by James Duncan Davidson, Michael Beam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

NSKeyValueCoding — Mac OS X 10.0

Synopsis

This informal protocol provides a way for clients to access a class’s instance variables (or other properties) without having to explicitly rely on accessor methods. NSKeyValueCoding provides indirect access to an object’s instance variables through the use of strings or keys. The two most commonly used methods in the protocol are takeValue:forKey: and valueForKey:, which are used to set and get the value of an instance variable, respectively. This protocol, and the associated NSScriptKeyValueCoding protocol, both form the basis of scripting in Cocoa. Chapter 2 goes into more detail about this protocol.

@interface NSObject (NSKeyValueCoding)
                                  // Class Methods
   + (BOOL)accessInstanceVariablesDirectly;
   + (BOOL)useStoredAccessor;
                                  // Instance Methods
   - (id)valueForKey:(NSString *)key;
   - (void)takeValue:(id)value 
                                 forKey:(NSString *)key;
   - (id)storedValueForKey:(NSString *)key;
   - (void)takeStoredValue:(id)value 
                                 forKey:(NSString *)key;
   - (id)valueForKeyPath:(NSString *)key;
   - (void)takeValue:(id)value 
                                 forKeyPath:(NSString *)key;
   - (NSDictionary *)valuesForKeys:(NSArray *)keys;
   - (void)takeValuesFromDictionary:(NSDictionary *)dictionary;
   - (id)handleQueryWithUnboundKey:(NSString *)key;
   - (void)handleTakeValue:(id)value 
                                 forUnboundKey:(NSString *)key;
   - (void)unableToSetNilForKey:(NSString *)key;
@end

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required