Getting a Value from NSDictionary
suggest changeThere are multiple ways to get an object from an NSDictionary with a key. For instance, to get a Lamborghini from a list of cars:
Standard
Car * lamborghini = [cars objectForKey:@"Lamborghini"];
Just like any other object, call the method of NSDictionary that gives you an object for a key, objectForKey:. Be careful not to confuse this with valueForKey:; that’s for a completely different thing, Key Value Coding
Shorthand
Car *lamborghini = cars[@"Lamborghini"];
This is subscript syntax, similar to dictionaries in C#, Java, and JavaScript. It has been standard Objective-C since Xcode 4.4, and it is only a shorter way to call objectForKeyedSubscript:.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents