Both methods are part of Key-Value Coding and should not normally be used to access an item in dictionaries. They work only with keys of type NSString
and require a specific syntax.
The difference between the two is that specifying one (one) key will simply look for this element.
An indication of a key path, on the other hand, follows a path through objects. If you have a dictionary of dictionaries, you can find the element at the second level using the key path, for example "key1.key2"
.
If you just want to access items in a dictionary, you should use objectForKey:
and setObject:forKey:
Edit to answer why valueForKey:
should not be used:
valueForKey:
only works for string keys. Dictionaries can use other objects for keys.valueForKey:
Handles keys that begin with the @ symbol differently. You cannot access items whose keys begin with @
.- Most importantly: using
valueForKey:
you say: "I am using KVC." There must be a reason for this. When using objectForKey:
you simply access the elements in the dictionary through a natural, targeted API.
source share