8 KVC, KVO, and Bindings

Key-value coding (KVC) is a mechanism that allows you to get and set the value of a property indirectly using a key. A key is the name of a property as a string.

KVC has been part of Cocoa for a long time, and important features of Cocoa, like Core Data and Cocoa bindings, rely on KVC. To understand and use these technologies, it helps to understand the basics of KVC.

To experiment with KVC, create a new playground. From Xcode’s File menu, select New...Playground. Name the playground KVC and save it with your previous exercises.

At the top of the file, define a Student class as a subclass of NSObject. Give it two variables with default values.

i​m​p​o​r​t​ ​C​o​c​o​a​ v​a​r​ ​s​t​r​ ​=​ ​"​H​e​l​l​o​,​ ...

Get Cocoa Programming for OS X: The Big Nerd Ranch Guide 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.