2

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!

1
  • 1
    isMemberOfClass checks if something is exactly that class. You want isKindOfClass instead, especially for "class clusters" like NSDictionary. The NSDictionary class 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. Commented Mar 7, 2012 at 0:04

2 Answers 2

3

Try out this link:

if([eventsArray isKindOfClass:[NSDictionary class]]) { //there's only one item in the XML } else { //there's more than one item in the XML } 

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

I'd do:

if ([eventsArray isKindOfClass:[NSArray class]]) //// else if ([eventsArray isKindOfClass:[NSDictionary class]]) //// else //// 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.