I've got this XML file that I am parsing using the parser here and I'm running into an issue. The parser takes the XML and creates a dictionary out of it if there is only one item in the XML with the same structure, or an array of dictionaries if there is more than one XML item. For example:
<root> <item> <attr1>Hello</attr1> <attr2>world!</attr2> </item> Would create a dictionary. But:
<root> <item> <attr1>Hello</attr1> <attr2>world!</attr2> </item> <item> <attr1>Hello</attr1> <attr2>world!</attr2> </item> Would create an array of dictionaries.
Now, how would I distinguish if the retrieved data is an NSDictionary or an NSArray? What do I set the results of the parser to? For example, right now I'm doing this:
id eventsArray = [[[[XMLReader dictionaryForXMLData:responseData error:&parseError] objectForKey:@"root"] objectForKey:@"events"] objectForKey:@"event"]; if([eventsArray isMemberOfClass:[NSDictionary class]]) { //there's only one item in the XML } else { //there's more than one item in the XML } But that doesn't work. So, how would I check what type of object eventsArray is?
Thanks!
isMemberOfClasschecks if something is exactly that class. You wantisKindOfClassinstead, especially for "class clusters" likeNSDictionary. TheNSDictionaryclass doesn't actually ever get used, instead there is a (very long) list of subclasses which are not documented and can change arbitrarily. You never really know which subclass you're going to get, and an object could theoretically even change from one class to another while you're using it.