我已经得到了这个 XML 文件,我正在使用解析器 here 来解析它。我遇到了一个问题。如果 XML 中只有一个具有相同结构的项目,则解析器会获取 XML 并从中创建一个字典;如果有多个 XML 项目,则解析器会创建一个字典数组。例如:
<root>
<item>
<attr1>Hello</attr1>
<attr2>world!</attr2>
</item>
将创建一个字典。但是:
<root>
<item>
<attr1>Hello</attr1>
<attr2>world!</attr2>
</item>
<item>
<attr1>Hello</attr1>
<attr2>world!</attr2>
</item>
将创建一个字典数组。
现在,我如何区分检索到的数据是 NSDictionary 还是 NSArray?我该如何将解析器的结果设置为什么?例如,现在我正在这样做:
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
}
但这行不通。那么,我如何检查 eventsArray 是什么类型的对象?
谢谢!
最佳答案
尝试此链接:
if([eventsArray isKindOfClass:[NSDictionary class]]) {
//there's only one item in the XML
} else {
//there's more than one item in the XML
}
希望这有帮助。
关于iphone - 在 Objective-C 中测试类的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9593803/