user979331 user979331 - 6 months ago 33
Objective-C Question

Objective-C dictionary argument is not an NSDictionary

I have this line of code here:

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


but this gives me an error when I run it:

dictionary argument is not an NSDictionary


when I do this:

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

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


I get no error and punchList gets populated, how come its not working the first way I did it.

Answer

It may be clearer if you break the code down into separate statements with separate variables. The failing example can be broken out as:

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];

The second set of statements can be broken down as:

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"];
Comments