Wednesday, 15 August 2012

objective c - Accessing keys in NSDictionary using [key] notation? -



objective c - Accessing keys in NSDictionary using [key] notation? -

i have realised can access nsdictionary using both objectforkey: , dict[key]?

nsdictionary *coordsdict = @{@"xpos": @5.0, @"ypos": @7.2, @"zpos": @15.7}; nslog(@"xpos: %@", coordsdict[@"xpos"]); nslog(@"xpos: %@", [coordsdict objectforkey:@"xpos"]);

can tell me if has been hiding me along or if recent alter language?

edit: question not generically refer new string literals, more accessing nsdictionary same string literal syntax utilize nsarray. overlooked , wanted check when particular syntax added.

this new add-on xcode 4.4+ , relies on apple's llvm+clang compiler. it's new feature :) arrays can accessed same notation: myobjectarray[4].

if you're interested in adding new feature own classes (called subscripting), there's few methods 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 implement of these methods on own classes, can subscript on them. how can add together feature os x 10.7 too!

objective-c nsdictionary objective-c-literals

No comments:

Post a Comment