Reputation: 161
I recently started using Faux Pas (http://fauxpasapp.com/), and for my project I get the following warning a number of times:
Old, verbose Objective-C syntax
-[NSDictionary objectForKey:] is called. Consider using an Objective-C subscript expression instead.
I'm unsure what the subscript expression is, and I'm not having much luck finding anything on it. I was curious if anyone here could help.
Thanks!
Upvotes: 1
Views: 2269
Reputation:
Well typically you would have written something like this:
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:42] forKey:@"foo"];
id value = [dictionary objectForKey:@"foo"];
And now you would write something like this:
NSDictionary *dictionary = @{@"foo": @42};
id value = dictionary[@"foo"];
Which, I think you will agree is a lot simpler and nicer to look at.
Some nice information about Object Subscripting can be found at NSHipster
Upvotes: 4
Reputation: 536026
You want to learn about Objective-C 2.0. The best way is to go straight to the source:
http://clang.llvm.org/docs/ObjectiveCLiterals.html
As you can see, a number of language features were introduced: NSNumber literals and "boxing" expressions, along with subscripting of NSArray and NSDictionary (and your own classes if you like). This is considered the "modern" way.
Xcode will refactor your code into "modern Objective-C" for you (see under Edit > Refactor), so you can modernize your code without doing any work!
Upvotes: 2
Reputation: 112873
There is no performance difference, just that the literal syntax is more clear, less verbose and has been available for several years now.
If the current code is like:
id var = [dictionary objectForKey:@"key"];
replace it with:
id var = dictionary[@"key"];
Upvotes: 6