Question

I have an NSDictionary of strings mapping to numbers that contains properties of an object to be edited in the UI, e.g.

  • Length : 1
  • Height : 2

Now trying to bind text fields in the UI to entries in the dictionary using the key of items in the dictionary in the key path fails miserably, so is trying to simply access entries in the dictionary with valueForKey: like

NSLog(@"KVC: %@", [self valueForKey:@"keysAndValues"]);

--> OK, Dump shows that Length is stored in the dictionary.

NSLog(@"KVC: %@", [self valueForKey:@"keysAndValues.Length"]);

[< YourClass 0x114608de0> valueForUndefinedKey:]:
this class is not key value coding-compliant for the key keysAndValues.Length.

Any suggestions on how to set up UI bindings with a pretty generic model having all it's stuff stored in a dictionary? I'm pretty sure I've read that KVC/KVO with paths to access individual entries in a dictionary actually works - but can't find the topic any more..

Was it helpful?

Solution

You want to use valueForKeyPath:, which takes a list of keys, separated by periods. Just using valueForKey can't include a series of keys as in your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top