I need to check if an dict has a key or not. How?
And what if the key does exist, but it's corresponding value is nil?
Fyodor Soikin
2010-05-06 21:30:32
That's not possible. You can't add nil to a NSDictionary. You would have to use `[NSNull null]` instead.
Ole Begemann
2010-05-06 21:39:17
@fyodor an nsdictionay will throw an NSInvalidArgumentException if you attempt to insert a nil value, so there should never be a case where a key exists, but the corresponding value is nil.
Brad Smith
2010-05-06 22:50:17
+2
A:
if ([mydict objectForKey:@"mykey"]) {
// key exists.
}
else
{
// ...
}
ChristopheD
2010-05-06 21:30:27
+2
A:
if ([[dictionary allKeys] containsObject:key) {
// contains key
}
or
if ([dictionary objectForKey:key]) {
// contains key
}
Aleksejs
2010-05-06 21:31:43