【问题标题】:Dictionary not copying in to other dictionary with exception: Dictionary argument is not a NSDictionary字典没有复制到其他字典,但有异常:字典参数不是 NSDictionary
【发布时间】:2012-12-13 14:46:14
【问题描述】:

我正在使用以下代码:

NSMutableDictionary *farmGatesDict  = [[NSMutableDictionary alloc] 
                 initWithDictionary:[xmlDictionary objectForKey:@"FarmGates"]];

        NSLog(@"value from dictionary : %@", [farmGatesDict objectForKey:@"FarmGate"]);


NSMutableDictionary *farmDetailDict=[[NSMutableDictionary alloc] 
                 initWithDictionary:[farmGatesDict objectForKey:@"FarmGate"]];

NSLog(@"%@", farmGateString);

我使用这个是因为这个字典是一个解析的 xml 文件,我想遍历树。我的第一个代码 farmGatesDict 运行良好,但第二个代码 farmDetailDict 应用程序因以下异常而崩溃:

-[NSDictionary initWithDictionary:copyItems:]: 字典参数不是 NSDictionary'

如果我落后,请建议我..

下面是 NSLog 数据...

2012-12-28 15:40:05.223 FarmGates[6170:c07] value from dictionary : (
        {
        address =         {
            text = "\n        2873 Huon Highwway, 2km south of Huonville, over the bridge on river side of road.";
        };
        contact =         {
            text = "\n        Contact 03 6264 1474";
        };
        description =         {
            text = "\n        Shed Door Sales. Arguably the longest standing and most popular road side stall in the Huon. Incorporated with a modern apple packing facility and featuring the refrigerated displays this ensures the freshest stock direct from the grower.";
        };
        fruits =         {
            fruit =             (
                                {
                    text = "\n        \n          Cherries";
                },
                                {
                    text = "\n          Apple";
                }
            );
            text = "\n        ";
        };
        id = 1;
        images =         {
            image =             (
                                {
                    text = "\n        \n          a1.jpg";
                },
                                {
                    text = "\n          b1.jpg";
                },
                                {
                    text = "\n          c1.jpg";
                }
            );
            text = "\n        ";
        };
        latitude =         {
            text = "\n        -43.04964";
        };
        longitude =         {
            text = "\n        147.03903";
        };
        name =         {
            text = "\n  \n  \n        Griggs Grower Direct apples and cherries";
        };
        open =         {
            text = "\n        Open daily except Christmas and Boxing Day.";
        };
        region =         {
            text = "\n        Huon Valley";
        };
        services =         {
            service =             {
                text = "\n        \n            NA";
            };
            text = "\n        ";
        };
        text = "\n    ";
    },
)

【问题讨论】:

  • 检查您的 [farmGatesDict objectForKey:@"FarmGate"] 是否返回字典。
  • @Ashu :检查我的答案....
  • 我遇到的问题是,当我使用包含嵌套标签的 xml 解析字典 (xmlDictionary) 时,当嵌套标签出现时,“(”符号即将到来,这不是字典的起始符号. 仍然我没有解决方案可以请任何人提出一些建议..

标签: iphone objective-c ios xml-parsing nsdictionary


【解决方案1】:

你得到一个数组,从NSLog 可以很容易地看到。

(
        {
        address =         {
            text = "\n        2873 Huon Highwway, 2km south of Huonvil

所以先使用objectAtIndex:,然后再使用objectForKey:

NSMutableDictionary *farmDetailDict=[[NSMutableDictionary alloc] initWithDictionary:farmGatesDict[0]];

【讨论】:

  • 我没有在我的应用程序中使用一个名为数组的单词,这个 NSLog 用于以下代码,仅用于字典。 NSLog(@"字典中的值:%@", [farmGatesDict objectForKey:@"FarmGate"]);
  • 字典的 Nslog 以 { 开头,而对于数组 (。这就是为什么我说它包含数组。你检查过我的代码了吗?如果仍然有错误,你可以把你的代码发给我吗?我的电子邮件 ID 在我的个人资料。
  • 那个标志来了,因为我是 xmlDictionary,它由来自 xml 文件的解析数据组成,每当嵌套标签出现时,就会出现这个标志。你是对的,问题是完全一样的,你能建议解决方案吗,因为我不能将代码作为项目数据发送
  • 尝试 nslogging 为 [... class],如果它提供的字典比酷,否则你必须采取其他方式。
【解决方案2】:

[farmGatesDict objectForKey:@"FarmGate"] 似乎没有返回 NSDictionary。检查它是什么类型。

【讨论】:

    【解决方案3】:

    只需在崩溃的行之前写下以下行..

    NSLog(@"value from dictionary : %@", [farmGatesDict objectForKey:@"FarmGate"]);
    

    并发布 NSLog 结果。我敢打赌你得到的值不是NSDictionary

    【讨论】:

      【解决方案4】:

      检查返回值

      farmGatesDict objectForKey:@"FarmGate"]
      

      试试NSLog

      【讨论】:

        【解决方案5】:

        我怀疑[xmlDictionary objectForKey:@"FarmGates"] 不会返回NSDictionary

        NSMutableDictionary *farmGatesDict  = [[NSMutableDictionary alloc] 
                         initWithDictionary:xmlDictionary];
        

        它应该可以工作。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-12-27
          • 2013-01-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-10-17
          • 2018-03-27
          相关资源
          最近更新 更多