views:

36

answers:

1

Following code works fine:

            NSDictionary *item;
            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??

+1  A: 

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

St3fan
Thanks. Even after changing to use nil instead of the [NSNull null], the line that's causing the crash though is the " NSString *mid = [[item objectForKey:@"/common/topic/article"] objectForKey:@"id"];". Is this syntax correct?
Wally