Check key exists in NSDictionary

29down votefavorite

3

how can I check if this exists?:

[[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"]

I want to know whether this key exists or not. How can I do that?

Thank you very much 🙂

EDIT: dataArray has Objects in it. And these objects are NSDictionaries.

iphone ios nsdictionary
share|improve this question
edited Jan 8 ’11 at 17:26
middaparka
31.7k45577
asked Jan 8 ’11 at 17:21
cocos2dbeginner
73411336
 
   
Any reason why you’re using -valueForKey: instead of -objectForKey:? The former is used in key-value coding and is slightly slower than the latter. –  Bavarious Jan 8 ’11 at 17:53

add comment

3 Answers

activeoldestvotes
up vote54down voteaccepted

I presume that [dataArray objectAtIndex:indexPathSet.row] is returning an NSDictionary, in which case you can simply check the result of valueForKey against nil.

For example:

if([[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] != nil) {
    // The key existed...

}
else {
    // No joy...

}
share|improve this answer
answered Jan 8 ’11 at 17:24
middaparka
31.7k45577
 
   
thanks. but so easy? I thought I must do some difficult things. lol –  cocos2dbeginner Jan 8 ’11 at 17:54
1  
+1 returning your vote on my answer to you. –  BoltClock♦ Jan 8 ’11 at 18:07

add comment

up vote18down vote

Check if it’s nil:

if ([[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] != nil) {
    // SetEntries exists in this dict
} else {
    // No SetEntries in this dict
}
share|improve this answer
answered Jan 8 ’11 at 17:23
BoltClock♦
213k42493721
 
   
hmm you just made a the excatly same answer like middaparka. –  cocos2dbeginner Jan 8 ’11 at 17:55
   
@cocos2dbeginner Pretty sure @BoltClock beat me to it actually. –  middaparka Jan 8 ’11 at 17:56
   
+1 For the guilt. 😉 –  middaparka Jan 8 ’11 at 17:56
   
@cocos2dbeginner: Yeah I was first to answer. Doesn’t matter whose gets accepted though. –  BoltClock♦Jan 8 ’11 at 17:58
4  
@cocos2dbeginner: If “He” refers to me, you’re right. –  BoltClock♦ Jan 8 ’11 at 18:08

show 2 more comments

up vote18down vote

So I know you already selected an answer, but I found this to be rather useful as a category onNSDictionary. You start getting into efficiency at this point with all these different answers. Meh…6 of 1…

- (BOOL)containsKey: (NSString *)key {
     BOOL retVal = 0;
     NSArray *allKeys = [self allKeys];
     retVal = [allKeys containsObject:key];
     return retVal;
}
share|improve this answer
edited Oct 26 ’12 at 22:11
Emil
3,37264198
answered Oct 26 ’12 at 19:16
Miles Alden
987616
 
   
I also wrote the same method in my category, but I have a weird crash happened when I use UIImageWriteToSavedPhotosAlbum(). after narrow down, I change the method name from containsKey => hasKey and the crash disappeared –  79bxh9b Sep 6 ’13 at 17:07 
   
This is much better than the accepted answer. I want to search by a key and not if the value of the key is nil or not. You could have a key in a dictionary that points to a nil value. Yet the key exists. The accepted answer doesn’t cover this scenario. But this solution here does. –  Hooman Oct 30 ’13 at 11:21
   
@Hooman: Not true about a key having a value of nil: the second paragraph of the NSDictionary documentation (developer.apple.com/library/mac/documentation/Cocoa/Reference/…) states that “Neither a key nor a value can be nil; if you need to represent a null value in a dictionary, you should use NSNull.” Both this and the accepted answer work for your scenario, i.e. with a dictionary where a value for some key is null (of type NSNull). –  Jarno Lamberg Jan 12 at 10:52
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s