Check key exists in NSDictionary

cocos2dbeginner picture cocos2dbeginner · Jan 8, 2011 · Viewed 95.7k times · Source

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.

Answer

John Parker picture John Parker · Jan 8, 2011

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

}