Chapter 11. Key-Value Coding and NSPredicate

 

This chapter covers

  • Key-Value Coding (KVC)
  • Handling nil values in KVC
  • Filtering a collection with predicates
  • Using key paths in predicate expressions

KVC is a feature of the Foundation framework that allows you to access the properties of an object using strings. To obtain the current value of an object’s property, you usually send the object an explicit getter message, as demonstrated here:

CTRentalProperty *house = ...;
NSString *addressOfProperty = [house address];

Given an instance of the CTRentalProperty class developed in chapter 5, this would return the current value of the house’s address property. In Objective C 2.0 you could also use the following alternative property accessor syntax:

NSString *addressOfProperty = house.address;

When using either syntax, it isn’t possible at runtime to change which property is accessed, because the property name is explicitly hardcoded into the source code. You can work around this potential problem by using an if statement:

id value;
if (someConditionIsTrue)
    value = [house address];
else
    value = [house propertyType];

11.1. Making your objects KVC-compliant

11.2. Handling special cases

11.3. Filtering and matching with predicates

11.4. Sample application

11.5. Summary