DevPrep

All topics › Objective-C

What are KVC and KVO?

Middle PRO

KVC — Key-Value Coding

Accessing properties by a string key: [obj valueForKey:@"name"], setValue:forKey:, key paths valueForKeyPath:@"address.city". The runtime looks for an accessor name/isName/setName:, and if there is none and accessInstanceVariablesDirectly = YES — the ivar _name/name. Scalars are wrapped in NSNumber/NSValue automatically. Collection operators: @"items.@sum.price", @count, @max, @distinctUnionOfObjects. Core Data, AppKit bindings and NSPredicate/NSSortDescriptor are built on KVC.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store