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.
If you wish to check if the NSDictionary contains any key (non-specific) you should use [dictionary allKeys]. count == 0 If the count is 0 there are no keys in the NSDictionary .
Sending a message to nil / NULL is always valid in Objective-C. So it depends if you want to check if a dictionary is nil , or if it doesn't have values (as a valid dictionary instance may be empty). In the first case, compare with nil . Otherwise, checks if count is 0, and ensure you're instance is still valid.
An object representing a static collection of key-value pairs, for use instead of a Dictionary constant in cases that require reference semantics.
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...
}
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;
}
Check if it's nil:
if ([[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] != nil) {
// SetEntries exists in this dict
} else {
// No SetEntries in this dict
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With