Objective-C Language Setting a Value in NSDictionary


Example

There are multiple ways to set a key's object in an NSDictionary, corresponding to the ways you get a value. For instance, to add a lamborghini to a list of cars

Standard

[cars setObject:lamborghini forKey:@"Lamborghini"];

Just like any other object, call the method of NSDictionary that sets an object of a key, objectForKey:. Be careful not to confuse this with setValue:forKey:; that's for a completely different thing, Key Value Coding

Shorthand

cars[@"Lamborghini"] = 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