Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c -[\uu NSCFDictionary objectAtIndex:]:发送到实例的选择器无法识别_Objective C_Json_Unrecognized Selector - Fatal编程技术网

Objective c -[\uu NSCFDictionary objectAtIndex:]:发送到实例的选择器无法识别

Objective c -[\uu NSCFDictionary objectAtIndex:]:发送到实例的选择器无法识别,objective-c,json,unrecognized-selector,Objective C,Json,Unrecognized Selector,这是我的密码: NSString *url = @"https://www.googleapis.com/language/translate/v2?key=API&q=hello%20world&source=en&target=de"; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; NSLog(@"%@",request); NSData *respo

这是我的密码:

NSString *url = @"https://www.googleapis.com/language/translate/v2?key=API&q=hello%20world&source=en&target=de";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSLog(@"%@",request);

NSData *response = [NSURLConnection sendSynchronousRequest:request
                                             returningResponse:nil
                                                         error:nil];
NSLog(@"%@",response);
NSError *jsonParsingError = nil;
NSArray *retrievedJTrans = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
NSLog(@"%@",retrievedJTrans);
NSDictionary *translation;
for(int i=0; i<[retrievedJTrans count];i++)
{
    translation=[retrievedJTrans objectAtIndex:i];
    NSLog(@"Statuses: %@", [translation objectForKey:@"translatedText"]);
}
NSLog(@"%@",[translation class]);
但我得到了一个错误:

-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance
感谢您的帮助。使用最新的Xcode。

请参阅这一行:

NSArray *retrievedJTrans = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
您假设返回给您的JSON是一个数组,但错误消息告诉您它是一个NSDictionary。您可以使用的一个小测试是:

id receivedObject = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
if ([receivedObject isKindOfClass:[NSDictionary class]]) {
    // Process the object as a dictionary
} else {
    // Process the object as an array
}
这是一本字典

你想要什么

NSDictionary *retrievedJTransD = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
NSArray *retrievedJTrans = retrievedJTransD[@"data"][@"translations"];

被许多人愚弄。。。你的json是一个dict,而不是一个数组。你的字典没有索引。一般来说,检查json序列化产生的对象类是个好主意@选民:为什么?
NSArray *retrievedJTrans = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
NSDictionary *retrievedJTransD = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
NSArray *retrievedJTrans = retrievedJTransD[@"data"][@"translations"];