1

As I know there is difference between objectForKey and valueForKey.

objectForKey: datatype is :(id)aKey whereas valueForKey: datatype is :(NSString *)key.

I know that this topic already discussed on stackoverflow but that is not provide me satisfaction. so please do not mark as duplicated

I already referred :

Difference between objectForKey and valueForKey?

What is the difference between valueforKey:, objectForKey:, and valueForKeyPath:?

Finally NSString is also type of id. so why apple provide valueForKey over objectForKey. i think there is some performance problem or any other. my question is if objectForKey satisfied all requirement of valueForKey then what is need of valueForKey?

Community
  • 1
  • 1
Jatin Patel - JP
  • 3,725
  • 2
  • 21
  • 43
  • It doesn't, what didn't you understand from looking at the first linked answer you gave - they are used for different things – Wain Apr 23 '15 at 06:49
  • what is difference situation where they are used separately? like once i used 'valueForKey' if i have `NSString` as `key`. in this place i can used `objectForKey` as well. is there any situation where they are not getting each other place? – Jatin Patel - JP Apr 23 '15 at 06:52
  • 1
    It isn't about the key type, it's about the receiver being a dictionary or any other object (so KVC) – Wain Apr 23 '15 at 06:55
  • 1
    so can you please show me the different situation where they are used separately means they are not taking each other place? – Jatin Patel - JP Apr 23 '15 at 06:58
  • 2
    the typo in the title of this question is hilarious – Fonix Apr 23 '15 at 07:07
  • As I understand it , `valueForKey:` is [KVO](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/Overview.html#//apple_ref/doc/uid/20001838-SW1), but can be used to return objects in an NSDictionary, obviously `NSDictionary` overrides the `valueForKey:` method. `objectForKey:` is the designated method for retrieving values from an `NSDictionary` see [here](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/index.html#//apple_ref/occ/instm/NSDictionary/valueForKey:) – Rob Sanders Apr 23 '15 at 08:48
  • @RASS, of course, but my question is `What is prons and cons when i am using objectForKey and valueForKey has data type NSString`? – Jatin Patel - JP Apr 23 '15 at 09:02
  • However, sometimes, there is a practical difference which I don't understand clearly yet. `valueForKey` didn't work in SOGo-3.1.4's code trying to call an unavailable "method" `ASProtocolVersion` on the `context` object (`EXCEPTION: NAME:NSInvalidArgumentException REASON:-[WOContext ASProtocolVersion]: unrecognized selector sent to instance`), whereas `objectForKey` works (and is the usual way to query the `context` object elesewhere in the code). See https://github.com/inverse-inc/sogo/pull/217/files – imz -- Ivan Zakharyaschev Jul 20 '16 at 11:59

0 Answers0