Getting a Value from NSDictionary

suggest change

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 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:.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents