6

I have just realised that I can access NSDictionary using both objectForKey: and dict[key]?

NSDictionary *coordsDict = @{@"xpos": @5.0, @"ypos": @7.2, @"zpos": @15.7};
NSLog(@"XPOS: %@", coordsDict[@"xpos"]);
NSLog(@"XPOS: %@", [coordsDict objectForKey:@"xpos"]);

Can anyone tell me if this has been hiding from me all along or if its some fairly recent change to the language?

EDIT: The question does not generically refer to the new string literals, but more specifically to accessing NSDictionary with the same string literal syntax you would use for NSArray. I obviously overlooked this and just wanted to check when this particular syntax was added.

jscs
  • 63,694
  • 13
  • 151
  • 195
fuzzygoat
  • 26,573
  • 48
  • 165
  • 294
  • 2
    They are fairly new "Objective-C Literals", but have been much-discussed here. Duplicate questions will follow. http://clang.llvm.org/docs/ObjectiveCLiterals.html – trojanfoe Feb 14 '13 at 15:40
  • The question should rather be: how could you've been hiding from this broadly discussed language extension coming within the last year. – Nikolai Ruhe Feb 14 '13 at 15:40
  • 1
    http://stackoverflow.com/questions/12120153/new-objective-c-literals-in-4-4 – trojanfoe Feb 14 '13 at 15:44
  • 1
    http://stackoverflow.com/questions/9693647/is-there-some-literal-dictionary-or-array-syntax-in-objective-c – trojanfoe Feb 14 '13 at 15:45
  • 1
    possible duplicate of [What are the details of "Objective-C Literals" mentioned in the Xcode 4.4 release notes?](http://stackoverflow.com/questions/9347722/what-are-the-details-of-objective-c-literals-mentioned-in-the-xcode-4-4-releas) – trojanfoe Feb 14 '13 at 15:46
  • Thank you, most of these seem to focus on the inclusion of object literals (i.e. @{} or @() and @[] for NSArray, very few bother to mention that @[] also works with NSDictionay which is why I believe I over looked it. Much appreciated ... – fuzzygoat Feb 14 '13 at 15:50

2 Answers2

13

This is a new addition to Xcode 4.4+ and relies on Apple's LLVM+Clang compiler. It's a new feature :) Arrays can also be accessed with the same notation: myObjectArray[4].

If you're interested in adding this new feature to your own classes (called subscripting), there's a few methods you can implement:

@interface NSArray(Subscripting)
- (id)objectAtIndexedSubscript:(NSUInteger)index;
@end

@interface NSMutableArray(Subscripting)
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)index;
@end

@interface NSDictionary(Subscripting)
- (id)objectForKeyedSubscript:(id)key;
@end

@interface NSMutableDictionary(Subscripting)
- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;
@end

If you implement any of these methods on your own classes, you can subscript on them. This is also how you can add this feature to OS X 10.7 too!

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
Aditya Vaidyam
  • 6,259
  • 3
  • 24
  • 26
  • @fuzzygoat Go wild with this: I added a category on `NSSegmentedControl` to make segment addition FAR easier programmatically. Little things like this make my day a lot more fun! :) – Aditya Vaidyam Feb 14 '13 at 22:40
0

Refer to "Modern Objective-C", introduced in iOS 6.

See the WWDC 2012 video: Migrating to Modern Objective-C.

So, no, the feature is not really new and you have overlooked it... ;-)

Mundi
  • 79,884
  • 17
  • 117
  • 140