taevanbat
taevanbat

Reputation: 435

What does forKey mean? What does it do?

I'm guessing that it gives the object that is being added to the NSMutableDictionary or NSDictionary a name to access it. But, I have to confirm it. Can anybody tell me?

Upvotes: 0

Views: 156

Answers (1)

BoltClock
BoltClock

Reputation: 724142

Dictionaries are data structures that contain key-value pairs. They're also known as hash tables. So yes, you use a key to refer to its corresponding value.

For the following dictionary:

// Pseudo-code, not actual Objective-C code, merely for illustration
// (This {} syntax would be really nice to have though...)
NSDictionary *dict = {
    @"one" => NSNumber (1),
    @"two" => NSNumber (2)
};

The following code yields 1:

NSNumber *one = [dict objectForKey:@"one"];
NSLog(@"%d", [one intValue]);

Upvotes: 6

Related Questions