带有“/”的objectForKey字符串崩溃我的应用程序

时间:2010-09-30 20:29:38

标签: iphone ios4

以下代码正常运行:

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

但如果我用“/ code / location / article”替换“article”,应用程序崩溃了。是什么赋予了??我错过了什么?

1 个答案:

答案 0 :(得分:2)

如果NSDictionary不包含特定元素,则objectForKey:将返回nil而不是[NSNull null]

因此,如果字典不包含您要查找的对象,那么您实际上正在进行比较,如nil != [NSNull null],它将始终评估为true。这可能不是你想要的。

(检查NSNull表示该条目在那里,但是具有空值。这对于例如JSON响应很常见。我不确定它在您的场景中是否常见。)

相关问题