开发者

objectForKey string with "/" crashes my app

开发者 https://www.devze.com 2023-01-18 02:29 出处:网络
Following code works fine: NSDictionary *item; 开发者_如何学JAVAif([arrTesting count] >0 ) item = [arrTesting objectAtIndex:0];

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

0

精彩评论

暂无评论...
验证码 换一张
取 消