14

How do I verify if a key exists on a NSDictionary?

I know how to verify if it has some content, but I want to very if it is there, because it's dynamic and I have to prevent it. Like in some cases it could happen to have a key with the "name" and is value, but in another cases it could happen that this pair of value don't exists.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
fabio santos
  • 275
  • 1
  • 5
  • 15

2 Answers2

25

The simplest way is:

[dictionary objectForKey:@"key"] != nil

as dictionaries return nil for non-existant keys (and you cannot therefore store a nil in a dictionary, for that you use NSNull).

Edit: Answer to comment on Bradley's answer

You further ask:

Is there a way to verify if this: [[[contactDetailsDictionary objectForKey:@"professional"] objectForKey:@"CurrentJob"] objectForKey:@"Role"] exists? Not a single key, because is a really giant dictionary, so it could exist in another category.

In Objective-C you can send a message to nil, it is not an error and returns nil, so expanding the simple method above you just write:

[[[contactDetailsDictionary objectForKey:@"professional"] 
                              objectForKey:@"CurrentJob"] 
                                objectForKey:@"Role"] != nil

as if any part of the key-sequence doesn't exist the LHS returns nil

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
CRD
  • 52,522
  • 5
  • 70
  • 86
  • 1
    For the chained lookup, you can also use KVC: `[contactDetailsDictionary valueForKeyPath:@"professional.CurrentJob.Role"]`. – Ken Thomases Dec 20 '14 at 01:42
  • There's a small typo in the 1st line of code, the method name is `objectForKey`, not `objectForkey`. – jamix Mar 27 '15 at 07:52
8

NSDictionary returns all of the keys as an NSArray and then use containsObject on the array.

NSDictionary* dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"object", @"key"];
if ([[dictionary allKeys] containsObject:@"key"]) {
  NSLog(@"'key' exists.");
}
sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161
Bradley M Handy
  • 603
  • 6
  • 15
  • Is there a way to verify if this: [[[contactDetailsDictionary objectForKey:@"professional"] objectForKey:@"CurrentJob"]objectForKey:@"Role"]] exists? Not a single key, because is a really giant dictionary, so it could exist in another category. – fabio santos Oct 26 '12 at 16:40
  • Substitute `dictionary` with a call to a method returning an `NSDictionary`. – Bradley M Handy Oct 26 '12 at 16:44
  • If the method returns the same thing, what's the need on doing that? Maybe that's a noob question, but I came from Front End and that Objective C is killing me slowly :P – fabio santos Oct 26 '12 at 16:50
  • I see what you mean. Yeah, you could just assign the value to the dictionary variable and keep the `if` condition the same. – Bradley M Handy Oct 26 '12 at 16:53
  • 2
    That reply is just awful. It's hard to imagine how you could do this in any more inefficient way. What a waste of memory and CPU time. – gnasher729 Dec 20 '14 at 00:32