2

我正在使用以下代码:

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    ";
    },
)
4

5 回答 5

3

你得到一个数组,从NSLog.

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

所以objectAtIndex:先用再去objectForKey:

NSMutableDictionary *farmDetailDict=[[NSMutableDictionary alloc] initWithDictionary:farmGatesDict[0]];
于 2012-12-28T10:36:08.343 回答
0

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

于 2012-12-28T10:07:08.803 回答
0

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

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

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

于 2012-12-28T10:07:45.357 回答
0

检查返回值

farmGatesDict objectForKey:@"FarmGate"]

试试看NSLog_

于 2012-12-28T10:08:24.777 回答
0

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

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

它应该工作。

于 2012-12-28T10:09:04.447 回答