Chapter 6. Using Key Value Coding and Key Value Observing

In This Chapter

  • Learning about key value coding

  • Writing KVD compliant accessors

  • Using KVC to simplify complex tasks

  • Observing changes to other objects using key value observing

  • Implementing manual and automatic KVO notifications

The Objective-C runtime provides you with a variety of advanced tools for not just interacting with the operating system frameworks, but also for interacting with the attributes of your code. One tool available to you is the concept I cover in this chapter called Key Value Coding. Key Value Coding, or KVC as it is often referred to in Objective-C circles.

Accessing Object Properties Using Key Value Coding

Key value coding gives you the ability to access attributes of your classes using a set of standardized accessor methods in addition to your normal setters and getters. You can use these accessor methods to get and set properties of your classes by specifying string identifiers that represent the names of the attributes you want to access. In addition to enabling you to access these attributes using these string identifiers, you can also access object relationships and sub objects by using a standardized syntax.

To give you an example of what I'm talking about, take a look at Listing 6.1

Example 6.1. Listing 6.1

Some example classes.

@interface Bar : NSObject
{
    NSArray *array;
    NSString *stringOnBar;
}
@property (retain, nonatomic) NSArray * array; @property (retain, nonatomic) NSString * stringOnBar; @end @interface ...

Get Objective-C now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.