繁体   English   中英

Objective-C字典参数不是NSDictionary

[英]Objective-C dictionary argument is not an NSDictionary

我在这里有这行代码:

punchList = [[[NSMutableDictionary alloc]initWithDictionary:[[[areaData GetPunchListDataArray:communityDesc] objectForKey:@"Root"] objectForKey:@"KeyValue"]] autorelease];

但是当我运行它时,这会给我一个错误:

dictionary argument is not an NSDictionary

当我这样做:

punchList = [[[NSMutableDictionary alloc]initWithDictionary:[[areaData GetPunchListDataArray:communityDesc] objectForKey:@"Root"]] autorelease];

    punchList = [punchList objectForKey:@"KeyValue"];

我没有得到任何错误,并且punchList被填充,为什么它不能正常工作我做的第一种方式。

如果将代码分解为具有单独变量的单独语句,则可能更清楚。 失败的例子可以分解为:

punchListDataArray = [areaData GetPunchListDataArray:communityDesc];
rootObject = [punchListDataArray objectForKey:@"Root"];

// This is not dictionary, so next line fails
possibleDictionary = [rootObject objectForKey:@"KeyValue"]; 
punchList = [[[NSMutableDictionary alloc]initWithDictionary:possibleDictionary] autorelease];

第二组陈述可以细分为:

punchListDataArray = [areaData GetPunchListDataArray:communityDesc];
rootObject = [punchListDataArray objectForKey:@"Root"]; 
// A different variable is being used to initialize
punchList = [[[NSMutableDictionary alloc]initWithDictionary:rootObject] autorelease];
punchList = [punchList objectForKey:@"KeyValue"];

仅仅因为行punchList = [punchList objectForKey:@"KeyValue"]; 没有给你一个错误并不意味着结果是正确的。

尝试这个:

punchList = [[[NSMutableDictionary alloc]initWithDictionary:[[areaData GetPunchListDataArray:communityDesc] objectForKey:@"Root"]] autorelease];
punchList = [punchList objectForKey:@"KeyValue"];
NSLog(@"punchList is now a %@", [punchList class]);
id someValue = [punchList objectoForKey:@"SomeKey"];

运行该代码应该会崩溃,并在某些类上显示有关未知选择器objectForKey:的消息。 并且控制台将显示日志消息,告诉您当时的punchList实际是什么。

原因与您的第一个代码块失败的原因相同。 那是因为结果:

[[[areaData GetPunchListDataArray:communityDesc] objectForKey:@"Root"] objectForKey:@"KeyValue"]

不是NSDictionary

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM