Following code works fine:
NSDictionary *item;
开发者_如何学JAVA if([arrTesting count] >0 )
item = [arrTesting objectAtIndex:0];
// The text "article" is what I'm searching for to ensure it exists
if([item objectForKey:@"article"] != [NSNull null])
{
NSString *mid = [[item objectForKey:@"article"] objectForKey:@"id"];
(more code below...)
But if I replace "article" with "/code/location/article" the app crashes. What gives?? What am I missing??
If a NSDictionary
does not contain a specific element, objectForKey:
will return nil
and not [NSNull null]
.
So, if the dictionary does not contain the object that you are looking for, you are essentially doing a comparison like nil != [NSNull null]
which will always evaluate to true. This is probably not what you want.
(Checking for NSNull
means that the entry is there, but has a null value. This is common for for example JSON responses. I am not sure if it is common in your scenario though.)
精彩评论