Why should I use KVC rather than the simple dot syntax when accessing object properties?

There's the option to go the long way, if an receiver class conforms to the NSKeyValueProtocol:

[myInstance setValue:[NSNumber numberWithInt:2] forKey:@"integerProperty"];

or the short way:

myInstance.integerProperty = 2;

what's the point of this KVC method? When is this useful?


First, those aren't the same, the second should be:

myInstance.integerProperty = [NSNumber numbwerWithInt:2];

if integerProperty is an NSNumber.

In general you use the second form when you are doing the most things. You use setValue:forKey: and valueForKey: when you want to dynamically choose the property to store things in. For instance, think about how valueForKeyPath: against an NSArray works (for reference, if you call -valueForKey: against an NSArray it will return an array where each object is the result of asking the corresponding object in that NSArray for that value:

- (NSArray *) valueForKey:(id)key { NSMutableArray *retval = [NSMutableArray array]; for (NSObject *object in self) { [retval addObject:[object valueForKey:key]]; } return retval; }

In the above case we were able to use valueForKey: to implement our function even though we do not know what the key is beforehand, since it is passed in as an argument.


  • UIButton set titleLabel setValue(newLabel, forKeyPath: “titleLabel”)
  • google-app-engine: How can I have a list of objects as property in another class?
  • Is there way to keep a text view and a slider in sync to a CGRect's float value with bindings?
  • Format string for NSPredicate with @avg collection operator
  • Redirect *.appspot.com to custom domain: Google app engine (Django)
  • How to shallow copy app engine model instance to create new instance?
  • Sorting a query by a ListProperty (NDB)
  • Using KVC operator e.g., @unionOfSets/@sum in IB's Value Binding Model Key Path
  • Disadvantages to high make job values
  • How to use Eclipse Mars to connect to Subversion
  • i can't add #region to .ashx in visual studio 2010
  • Update progress bar from Task.Run async
  • C++ std::set comparator
  • JQuery Add class to certain list item
  • Change border corlor of NSTableView
  • Download/Save/Write a file on the client's hard disk using flash/flex
  • Catching exception and throwing the same?
  • SIP API media codecs
  • What Makes These Two Array Adds Different?
  • SyntaxError: (irb):26: both block arg and actual block given
  • How do I display a dialog that asks the user multi-choice questıon using tkInter?
  • Memory error in python- how to use more memory
  • How do I signal completion of my dataflow?
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • Parsing a CSV string while ignoring commas inside the individual columns
  • Avoid links criss cross / overlap in d3.js using force layout
  • Highlight one bar in a series in highcharts?
  • Scrapy recursive link crawler
  • How to rebase a series of branches?
  • Initializer list vs. initialization method
  • Android screen density dpi vs ppi
  • Spring security and special characters
  • Nant, Vault & Windows Integrated Authentication
  • How would I use PHP exceptions to define a redirect?
  • How to extract text from Word files using C#?
  • Trying to switch camera back to front but getting exception
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • need help with bizarre java.net.HttpURLConnection behavior
  • Programmatically clearing map cache