0

当我得到超过 1 个对象时,一切正常,但是当它只有 1 个时,它的反应很奇怪,我找不到它的解决方案。

首先,我将所有内容都设置在一个数组中:

NSArray *array = [[[dictionary objectForKey:@"Response"] objectForKey:@"objecten"] objectForKey:@"object"];
if (array == nil) {
    NSLog(@"Expected 'results' array");
    return;
}

然后我在字典上使用 for 循环

for (NSDictionary *resultDict in array) {

    SearchResult *searchResult;


NSString *wrapperType = [resultDict objectForKey:@"type"];    

 if ([wrapperType isEqualToString:@"rent"]) 
 {
        searchResult = [self parseHuur:resultDict];

 }

        if (searchResult != nil) {
            [searchResults addObject:searchResult];
        }}

因此,当结果返回超过 1 时,一切正常,但是当只有一个返回时,我得到:

-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x6e52c30
*** Terminating app due to uncaught exception 
'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: 
unrecognized selector sent to instance 0x6e52c30'

它指向这一行:

NSString *wrapperType = [resultDict objectForKey:@"type"];    

我真的不明白...我在浏览器中使用相同的链接检查了 api 的结果,它确实返回了 1 个对象,但是当我记录 resultDict(NSlog 它)时,我只得到一个答案:id 而不是带有所有参数的整个对象(我不知道这是否是正确的名称)

怎么可能 ?

4

5 回答 5

2

您的一些结果不是完整的 NSDictionaries,而只是 NSStrings。你可以检查一下:

for (id result in array) {
   if ([result isKindOfClass:[NSDictionary class]]) {
      NSDictionary *resultDict = (NSDictionary *)result;
   ...
于 2012-11-14T18:32:16.923 回答
1

当您对 NSDictionary 使用快速枚举时,迭代变量来自字典中的键集而不是值。

http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/Chapters/ocFastEnumeration.html

于 2012-11-14T18:34:57.787 回答
1

根据您的评论,array并不总是您提到的数组。它可以是数组或字典。所以试试这个,

id someObject = [[[dictionary objectForKey:@"Response"] objectForKey:@"objecten"] objectForKey:@"object"]; //naming it as someObject since it is not always an array

if (someObject == nil) {
    NSLog(@"Expected 'results' array");
    return;
}

if ([someObject isKindOfClass:[NSDictionary class]]) { //Just add this
    someObject = [NSArray arrayWithObject:someObject];
} 

NSArray *array = (NSArray *)someObject;//type cast to an array now

for (NSDictionary *resultDict in array) {

   SearchResult *searchResult;
   NSString *wrapperType = [resultDict objectForKey:@"type"];    

   if ([wrapperType isEqualToString:@"rent"]) 
   {
        searchResult = [self parseHuur:resultDict];
   }
   if (searchResult != nil) {
        [searchResults addObject:searchResult];
   }
}
于 2012-11-15T01:05:03.580 回答
0

resultDict 不是 NSDictionary,因此您不能在此对象上调用 objectForKey:。

更好的解决方案是在 for 循环中将 resultDict 视为id 类型,并在使用它之前检查其类类型是否为 NSDictionary。

于 2012-11-14T18:33:53.343 回答
0
-[__NSCFString objectForKey:]

所以它objectForKey:在一个NSString. 您用于获取对象的 API 似乎遵循一个常见的习惯用法:它使用鸭子类型/多态(使用这些与 OO 相关的漂亮词),如果它有多个结果,它会返回一个对象数组,或单个对象(而不是一个元素的数组),当它只有一个结果时。因此,您必须使用反射(OMG,甚至更多 OO 术语!)来检查返回的对象是否实际上是一个数组 - 要么

id result = [dictionary objectForKey:@"Response"];
id value;
if ([result isKindOfClass:[NSDictionary class]]) {
    value = [[result object objectForKey:@"objecten"] objectForKey:@"object"];
} else {
    value = result;
}

或者

id value;
if ([dictionary isKindOfClass:[NSDictionary class]]) {
    value = [[[dictionary objectForKey:@"Result"] result object objectForKey:@"objecten"] objectForKey:@"object"];
} else {
    value = dictionary;
}

两者都试一下,哪个都行。

于 2012-11-14T18:34:52.800 回答