Check key exists in NSDictionary

IosObjective CIphoneNsdictionary

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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...

}

Solution 2 - Ios

So I know you already selected an answer, but I found this to be rather useful as a category on NSDictionary. 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;
}

Solution 3 - Ios

Check if it's nil:

if ([[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] != nil) {
    // SetEntries exists in this dict
} else {
    // No SetEntries in this dict
}

Solution 4 - Ios

this also works using Objective-C literals using the following syntax:

NSDictionary *dict = @{ @"key1" : @"value1", @"key2" : @"value2" };
if (dict[@"key2"])
NSLog(@"Exists");
else
NSLog(@"Does not exist");

Solution 5 - Ios

Try this:

if ([dict objectForKey:@"bla"]) {
   // use obj
} else {
   // Do something else like create the object
}

Solution 6 - Ios

This one does the same but with less code:

if (dataArray[indexPathSet.row][@"SetEntries"] != nil) { /* the key exists */        } 
else                                                   { /* the key doesn't exist */ }

Solution 7 - Ios

if ((NSNull *)[[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] != nil) {
    // SetEntries exists in this dict
} else {
    // No SetEntries in this dict
}

That's the right answer.

Solution 8 - Ios

Check dictionary contains any value. I prefer [dic allKeys].count > 0 to check.

Solution 9 - Ios

Use the (unsigned long) option:

if ( (unsigned long)[[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] ) {
    // Key exist;
}else{
    // Key not exist;
};

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncocos2dbeginnerView Question on Stackoverflow
Solution 1 - IosJohn ParkerView Answer on Stackoverflow
Solution 2 - IosMiles AldenView Answer on Stackoverflow
Solution 3 - IosBoltClockView Answer on Stackoverflow
Solution 4 - IosYogesh KumarView Answer on Stackoverflow
Solution 5 - IosTeaView Answer on Stackoverflow
Solution 6 - IosJesusView Answer on Stackoverflow
Solution 7 - IosWalterView Answer on Stackoverflow
Solution 8 - IoszedzhaoView Answer on Stackoverflow
Solution 9 - IosTheAlphaGhostView Answer on Stackoverflow