【问题标题】:What would cause objectForKey: to return null with a valid string in place?什么会导致 objectForKey: 返回带有有效字符串的 null ?
【发布时间】:2010-04-18 04:15:43
【问题描述】:

我遇到NSDictionaryNSString 返回null 的问题,即使该字符串在字典中。代码如下:

- (void)sourceDidChange:(NSNotification *)aNote {
    NSDictionary *aDict = [aNote userInfo];
    DLog(@"%@", aDict);
    NSString *newSourceString = [aDict objectForKey:@"newSource"];
    DLog(@"%@", newSourceString);
    newSourceString = [newSourceString stringByReplacingOccurrencesOfString:@" " withString:@""];
    DLog(@"%@", newSourceString);
    NSString *inspectorString = [newSourceString stringByAppendingString:@"InspectorController"];
    DLog(@"%@", inspectorString);
    newSourceString = [newSourceString stringByAppendingString:@"ViewController"];
    DLog(@"%@", newSourceString);
}

我得到以下日志语句:

2010-04-17 23:50:13.913 CoreDataUISandbox[13417:a0f] -[RightViewController sourceDidChange:] { newSource = "Second View"; }
2010-04-17 23:50:13.914 CoreDataUISandbox[13417:a0f] -[RightViewController sourceDidChange:] (null)
2010-04-17 23:50:13.916 CoreDataUISandbox[13417:a0f] -[RightViewController sourceDidChange:] (null)
2010-04-17 23:50:13.917 CoreDataUISandbox[13417:a0f] -[RightViewController sourceDidChange:] (null)
2010-04-17 23:50:13.917 CoreDataUISandbox[13417:a0f] -[RightViewController sourceDidChange:] (null)

如您所见,字符串在字典中的键 newSource 下,但是当我调用 objectForKey: 时,我得到了 null。我什至尝试过清理项目的后备选项。

有没有人遇到过这种情况,或者我只是忘记了一些非常基本的东西?

【问题讨论】:

  • 出于好奇,在设置并记录aDict 之后,如果你这样做DLog(@"%@", [aDict allKeys]); 会发生什么?
  • 这是实际代码吗?你有没有可能打错密钥? DLog(@"%@", [aDict allKeys]) 给了什么?
  • 您可能还想尝试使用%p 而不是%@ 记录newSourceString
  • 我知道我做了一些昨晚找不到的蠢事。原来我已经修改了我在 iphoneincubator.com 上找到的 DLog 版本,没有列出行号,只是我只删除了 LINE 部分而不是 [Line %d] 部分。当然,后来我忘记了我已经这样做了。我不确定为什么它只会导致 NSStrings 有问题被记录,而不是 NSDictionary。感谢您的帮助。

标签: objective-c cocoa nsstring nsdictionary


【解决方案1】:

此时,由于某种原因,您会收到来自 DLog 的报告错误。

试试:

  1. 使用 NSLog 进行日志记录。
  2. 在代码运行时直接在调试器中检查newSourceString 的值。

【讨论】:

    【解决方案2】:

    什么会导致objectForKey: 返回带有有效字符串的空值?

    两件事之一:

    1. 字典不包含该键的对象。 (您是否认为它是无关紧要的。)
    2. 您没有字典; aDictnil,因此您将 objectForKey: 消息发送到 nil。发给nil 的消息除了返回nil 什么都不做。

    如您所见,字符串在字典中的键 newSource...

    实际上,我不确定那里发生了什么。 NSDictionary 的描述(如果它包含该键的字符串)将是 { newSource = "some string here"; },它与您记录的描述不匹配。另一方面,如果它是一个不是字典的对象,您应该在尝试向其发送objectForKey: 消息时收到“不响应选择器”异常。因此,从您的日志输出来看,虽然它确实显示为 something,但我不知道它是什么,除了它可能不是字典。

    那就太奇怪了。

    【讨论】:

    • 每次我放入花括号时,它们都会搞砸代码块,所以我不得不删除它们(仍然不确定为什么会这样)。
    • 我已根据上面@theMikeSwan 的评论更新了问题以在日志语句中包含大括号。
    【解决方案3】:

    我遇到了类似的问题。对我来说,问题是我认为我的密钥是一个 NSString,而实际上它是一个 NSNumber。您可以使用以下方法检查您的密钥

    for (id key in [aDict allKeys]){
            NSLog(@"%@:%@",key, [[key class] description]);
        }
    }
    

    【讨论】:

      【解决方案4】:

      你没有忽视你编码的环境。如果是 GNUstep,特别是 gnustep1.19,请继续阅读。否则忽略。

      我刚刚在 gnustep1.19(.3) 中遇到了一个非常奇怪的错误,但它完美地模仿了这个问题。

      NSString * key = <some string>
      NSDictionary * dict = <some dictionary>
      
      (gdb) p [dict objectForKey:key]
      $20 = (struct objc_object *) 0x0
      (gdb) p [dict objectForKey:@"MyKeyValue"]
      $22 = (struct objc_object *) 0x7fffd94fe690
      (gdb) p [key compare"@MyKeyValue"]
      $25 = NSOrderedSame
      

      在这种情况下,“key”是通过从另一个 NSDictionary 中提取来初始化的,并且另一个字典中的一些条目(从文件加载)包含 Unicode 字符。也就是说,到目前为止,我发现的唯一相关性 - 从源文件中删除 unicode 并重新运行应用程序使其工作。

      这不是 gnustep1.18 或 >=gnustep1.20 的问题

      【讨论】:

        【解决方案5】:

        我怀疑您的字符串实际上是“(null)” - 也就是说,它有 6 个字母长,并且拼写为 (-n-u-l-l-)。

        【讨论】:

        • 打印输出的第一行显示该键确实具有“第二视图”的值。
        • 如果是这种情况,那么应用stringByAppendingString: 的步骤不应该仍然产生(null)
        • 没人觉得这很有趣吗?总有一天我会用这个来对我的搭档恶作剧=)
        【解决方案6】:

        我怀疑 aDict 不是 NSDictionary 的实例。 记录它的课程以确认。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-02-26
          • 2018-02-27
          • 2017-09-20
          • 2016-11-30
          • 2017-02-05
          • 1970-01-01
          • 2019-10-04
          • 1970-01-01
          相关资源
          最近更新 更多