An NSDictionary has a validation key.

how can i check if this exists ?:

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

I want to know if this key exists or not. How can i do this?

Thank you very much:)

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

+73
ios objective-c iphone nsdictionary
Jan 08 '11 at 17:21
source share
9 answers

I assume that [dataArray objectAtIndex:indexPathSet.row] returns an NSDictionary , in which case you can simply check the result for valueForKey nil.

For example:

 if ([[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] != nil) { // The key existed... } else { // No joy... } 
+155
Jan 08 2018-11-11T00:
source share

So, I know that you have already selected the answer, but I found it to be quite useful as a category on NSDictionary . You begin to receive efficiency at this moment with all these various answers. Fur ... 6 of 1 ...

 - (BOOL)containsKey: (NSString *)key { BOOL retVal = 0; NSArray *allKeys = [self allKeys]; retVal = [allKeys containsObject:key]; return retVal; } 
+46
Oct 26
source share

Check non zero:

 if ([[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] != nil) { // SetEntries exists in this dict } else { // No SetEntries in this dict } 
+34
Jan 08 2018-11-11T00:
source share

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"); 
+8
Dec 14 '14 at 11:28
source share
 if ((NSNull *)[[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] != nil) { // SetEntries exists in this dict } else { // No SetEntries in this dict } 

Correct answer.

0
Mar 12 '14 at 11:16
source share

Try the following:

 if ([dict objectForKey:@"bla"]) { // use obj } else { // Do something else like create the object } 
0
Apr 21 '14 at 14:20
source share

This does the same, but with less code:

 if (dataArray[indexPathSet.row][@"SetEntries"] != nil) { /* the key exists */ } else { /* the key doesn't exist */ } 
0
Oct 27 '14 at 19:25
source share

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

0
Oct 31 '16 at 2:27
source share

Use the (unsigned long) parameter:

 if ( (unsigned long)[[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] ) { // Key exist; }else{ // Key not exist; }; 
0
Jun 04 '19 at 17:31
source share



All Articles