Search code examples
objective-cparse-platformobjective-c-literals

How does PFObject allow subscripting like NSMutableDictionary?


I was reading Parse's iOS Developers Guide. I got confused where it shows PFObject assigning to keys with subscript syntax.

PFObject *gameScore = [PFObject objectWithClassName:@"GameScore"];
gameScore[@"score"] = @1337;
gameScore[@"playerName"] = @"Sean Plott";
gameScore[@"cheatMode"] = @NO;


PFObject *gameScore = [PFObject objectWithClassName:@"GameScore"];
gameScore[@"score"] = @1337;

First I thought that PFObject must be extending NSMutableDictionary, but PFObject is extending NSObject only. How it is behaving like NSMutableDictionary?

How can I do this for my own class?


Solution

  • NSHipster has the answer:

    Similarly, custom-keyed subscripting can be added to your class by declaring and implementing these methods:

    - (id)objectForKeyedSubscript:(*KeyType*)key;
    - (void)setObject:(id)obj forKeyedSubscript:(*KeyType*)key;