Objective-C Language NSDictionary Getting a Value from NSDictionary

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9

Example

There 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 the syntax that you use for dictionaries in most other languages, such as C#, Java, and Javascript. It's much more convenient than the standard syntax, and arguably more readable (especially if you code in these other languages), but of course, it isn't standard. It's also only available in newer versions of Objective C



Got any Objective-C Language Question?