iphone - 在 Objective-C 中测试类的类型

标签 iphone objective-c cocoa-touch casting

我已经得到了这个 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/

相关文章:

iphone - iPad 中的洪水填充速度太慢

iphone - 多线程和 “just leaking”警告

iOS - 点击后弹出自定义 subview

objective-c - 是否可以定义一个符合协议(protocol)的Class类型的属性?

objective-c - 如何获得 UIImageView 的图形上下文?

ios - 如何在 UICollectionView IOS 中一次只选择一张图像

iphone - 使用 Storyboard时如何获取指向 appDelegate 内的 viewController 的指针

iphone - 为什么我的 opengl-es 纹理显示为白色?

iphone - 在界面构建器的 UILabel 中使用本地化文本?

android - 难以在移动应用程序的 native 或跨平台之间做出选择