I am trying to store a variety of strings and a NSMutableArray in a NSMutableDictionary. Basically an XML element with various key/values and one subelement with a list.
NSMutableArray *subItem = [[[NSMutableArray alloc] init] autorelease];
for (GDataXMLElement *subElement in [key children]){
[subItem addObject:[[[self runThroughElement:subElement] copy] autorelease]];
}
[item setObject:[[subItem copy] autorelease] forKey:[key name]];
When I check the type:
NSLog(@"Categories type: %@", [[item objectForKey: @"categories"] classForCoder]);
I get:
Categories type: NSArray
But then in the function which deals with the data and stores it in an object it suddenly appears to be a string:
NSLog(@"Categories type: %@", [[obj objectForKey: @"categories"] classForCoder]);
Which results in:
Categories type: NSString
I am at a loss what is going wrong, why do I get a string out of the dictionary and not the array I put into it? Any help would be really appreciated!
(probably need more info, so please let me know what is relevant)
You shouldn’t really be using classForCoder. Instead just log [[obj objectForKey:@”categories”] class]. I’d also try logging the straight [obj objectForKey:@”categories”] to see if it does output a string or an array. If it outputs a string then either obj is not item or your dictionary is being modified at some point.
One bit of advice, there isn’t any reason to do [[[NSMutableArray alloc] init] autorelease]. Just call [NSMutableArray array]. Does the same thing but with less code. Same goes for many classes, look for convenience class methods to save you having to do alloc/init/autorelease.