简体   繁体   English

通过NSDictionary迭代时处理不同的对象类型

[英]Dealing with different object types while iterating through an NSDictionary

for (NSArray *values in [serializedJSON allValues])

Sometimes the values in serializedJSON will be arrays, and sometimes they will be NSDictionaries. 有时serializedJSON中的值将是数组,有时它们将是NSDictionaries。 I would like to discriminate against one of them so I don't get any errors like I am now. 我想区别其中之一,因此不会像现在这样出现任何错误。 So I only want the returning values in this case to be NSArrays, while in a second case I would only want them to be NSDictionaries. 所以我只希望在这种情况下返回的值是NSArrays,而在第二种情况下,我只希望它们是NSDictionaries。

Thanks in advanced! 提前致谢!

If you need more info let me know 如果您需要更多信息,请告诉我

The standard, generic way to handle JSON is roughly as follows: 处理JSON的标准通用方法大致如下:

NSObject* jsonResult = [serializedJSON allValues];
if ([jsonResult isKindOfClass:[NSArray class]]) {
   <handle NSArray>
}
else if ([jsonResult isKindOfClass:[NSDictionary class]]) {
   <handle NSDictionary>
}
else if ([jsonResult isKindOfClass:[NSNumber class]]) {
   <handle NSNumber>
}
else if ([jsonResult isKindOfClass:[NSString class]]) {
   <handle NSString>
}
else if (jsonResult == [NSNull null]) {
   <handle null>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM