14

I don't understand the differences between these ways of accessing NSDictionary values

[my_dict objectForKey:@"field"]
[my_dict valueForKey:@"field"]
my_dict[@"field"]

Can someone tell me ?

Popeye
  • 11,839
  • 9
  • 58
  • 91
Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307

1 Answers1

55

[my_dict objectForKey:@"field"] is an NSDictionary method. It accepts any type of object.

[my_dict valueForKey:@"field"] is KVC method. It accepts only NSString.

my_dict[@"field"] is same as objectForKey:. This is new feature added.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • Just seen this. Take a look at https://clang.llvm.org/docs/ObjectiveCLiterals.html#dictionary-style-subscripting for a full discussion on the subject of literals and subscripting. – Armand Jan 09 '18 at 07:03