Objective-C - 将NSDictionary中的项添加到NSMutableDictionary中

时间:2012-04-13 13:06:39

标签: objective-c ios json nsdictionary nsmutabledictionary

我在尝试将对象从NSDictionary加载到NSMutableDictionary时遇到问题 - 代码如下:

dictListData = [[NSMutableDictionary alloc] initWithCapacity:200];

...

NSError *jsonError = nil;
NSDictionary *jsonResultDict = [NSJSONSerialization JSONObjectWithData:[httpRequestCopy responseData] options:kNilOptions error:&jsonError];
NSDictionary *tempDataset = [[jsonResultDict objectForKey:@"result"] objectAtIndex:0]; 

NSLog(@"tempDataset = %@", tempDataset);

[dictListData addEntriesFromDictionary:tempDataset];   // <-- Error here

NSLog命令以字典样式格式输出数据,因此我知道数据存在,但addEntriesFromDictionary命令失败并显示错误:

'NSInvalidArgumentException' [NSMutableDictionary addEntriesFromDictionary:]: dictionary argument is not an NSDictionary'.

我试过对tempDataset进行类型转换,但没有区别。有什么想法吗?

我正在使用Xcode 4.3.2,目标是iOS SDK 5.1,启用了ARC。

jsonResultDict = {
    result =     (
                (
                        {
                stCode = 000477065;
                stDesc1 = "TIMER KNOB";
                stQtyFree = 0;
                stQtyInStock = 0;
            },
                        {
                stCode = 0010311;
                stDesc1 = "DBLE TAPER CHUCK CAP";
                stQtyFree = 10;
                stQtyInStock = 10;
            },
                        {
                stCode = "0016-103";
                stDesc1 = "1/8 I.D";
                stQtyFree = 0;
                stQtyInStock = 0;
            },
                        {
                stCode = "0016-104";
                stDesc1 = "1/4 I.D";
                stQtyFree = 0;
                stQtyInStock = 0;
            },
                        {
                stCode = "0016-157";
                stDesc1 = "1/8 x 1/16 X 1/8 TEE";
                stQtyFree = 0;
                stQtyInStock = 0;
            }
        )
    );
}

2 个答案:

答案 0 :(得分:2)

似乎tempDatasetNSArray类。

答案 1 :(得分:0)

Flink的回答是它是一个NSArray类应该是正确的。另外,如果您确实需要将NSDictionary转换为NSMutableDictionary,则更容易使用myDictionary.mutableCopy