0

I don't want to subclass from NSDictionary but I want to get the literal syntax. It means that I am about to save and retrieve the key by using the "[]". I get this question from Parse iOS SDK, the PFObject is not inherited from the NSMutableDictionary. And they are able to use the dictionary literal syntax.

Here is the link from Parse iOS SDK: https://parse.com/docs/ios_guide#objects/iOS.

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29
  • Do you mean using id *var = dict[@"key"] instead of id *var = [dict valueForKey:@"key"] and dict[@"key"] = var instead of [dict setValue:var forKey:@"key"]? – Victorianec Dec 12 '14 at 08:09
  • @VictorIppolitov: Beware! [The `valueForKey:` and `setValue:forKey:` methods should **not** be used to access a dictionary!](http://stackoverflow.com/questions/1062183/difference-between-objectforkey-and-valueforkey). Use `objectForKey:` and `setObject:forKey:` instead. – DarkDust Dec 12 '14 at 08:13
  • @DarkDust Thank you. I will keep in mind it. – Victorianec Dec 12 '14 at 08:19

1 Answers1

6

See for example the good Object Subscripting article at NSHipster. You just need to implement two methods in your class:

- (id)objectForKeyedSubscript:(id <NSCopying>)key;
- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;
DarkDust
  • 90,870
  • 19
  • 190
  • 224