objectForKey
objectForKey: is an instance method of NSDictionary and NSMutableDictionary in the Foundation framework. It returns the value associated with the specified key, or nil if the key is not present. The method is declared as - (id)objectForKey:(id)key. It is used to look up values in dictionary containers that map keys to values. Keys in NSDictionary must conform to NSCopying because dictionaries copy keys when adding them; they are compared using isEqual: and hashed using hash. When you call objectForKey:, the dictionary uses the key's hash to locate the bucket and then checks equality to find the exact entry.
In manual memory management environments, the returned object is autoreleased; with ARC the caller just uses
Common usage: NSDictionary *dict = @{@"name": @"Alice", @"age": @30}; id name = [dict objectForKey:@"name"]. You can also use
In addition to NSDictionary, NSUserDefaults also provides objectForKey:, used to retrieve stored preferences; its semantics are
See also: NSDictionary, NSMutableDictionary, NSUserDefaults.