Unrecognized selector error when indexing into a dictionary of arrays

前端 未结 1 559
抹茶落季
抹茶落季 2021-01-24 09:43

I have a dictionary of arrays that is causing __NSCFDictionary objectAtIndex: errors.

Can someone tell me why? The dictionary clearly has at least 1 array a

相关标签:
1条回答
  • 2021-01-24 10:18

    In your case [[json objectForKey:@"words"] objectForKey:@"word"]; is returning a dictionary and not an array. Try doing the following,

    id wordParam = [[json objectForKey:@"words"] objectForKey:@"word"];
    
    if ([wordParam isKindOfClass:[NSArray class]]) {
      NSDictionary *word = [(NSArray *)wordParam objectAtIndex:0];
    } else if ([wordParam isKindOfClass:[NSDictionary class]]) {
      NSDictionary *word = (NSDictionary *)wordParam;
    } else {
      NSLog(@"error. %@ is not an array or dictionary", wordParam);
    }
    

    Your response string also shows that value for word is,

    {"rowsreturned":0,"id":"-1","date":"","word":"","term":"","definition":"","updated_on":""}
    

    which is a dictionary with key value pairs as rowsreturned:0, id:-1 etc..

    0 讨论(0)
提交回复
热议问题