Key Value Coding

Key Value Coding (KVC) is one of the cornerstones of Cocoa Bindings. KVC allows us to access the attributes of an object without calling the accessors of that object directly. Key Value Coding is implemented through an informal protocol on NSObject itself and is used mainly through the getter/setter pair valueForKey(:) and setValue(:forKey:).

valueForKey(:)

The method valueForKey(:) is a generic accessor that retrieves an attribute on an object. For example, if we had an object called Recipe and it had an attribute called name, normally we’d access that attribute via the following:

 let​ myRecipe = ...
 let​ recipeName = myRecipe.name

However, this requires specific knowledge about the Recipe object to exist in the calling ...

Get Core Data in Swift now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.