在用于iPhone SDK的Youtube API中,我正在尝试获取每个搜索结果的videoID.我每次都会收到错误,我该怎么办?

mao*_*r10 0 iphone search youtube-api

这是我的代码:

  // Create a service object for executing queries
GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
// Services which do not require sign-in may need an API key from the
// API Console
service.APIKey = @"AIzaSyD9pvsUtnegJvwv5z5XrBO5vFTBVpErYN8";
// Create a query
GTLQueryYouTube *query = [GTLQueryYouTube queryForSearchListWithPart:@"id"];
query.maxResults = 50;
query.q = @"hiking boots";
//query.country = @"US";
// Execute the query
GTLServiceTicket *ticket = [service executeQuery:query
                               completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
                                   // This callback block is run when the fetch completes
                                   if (error == nil) {
                                       GTLYouTubeSearchListResponse *products = object;


                                       // iteration of items and subscript access to items.
                                       for (GTLYouTubeSearchResult *item in products) {

                                           //NSLog(@"%@",item.identifier); - THIS WORKS, BUT GIVES ME THE WHOLE IDENTIFIER, I JUST WANT THE VIDEO ID
                                           NSLog(@"%@",item.identifier.videoId);
                                       }
                                   }else{
                                       NSLog(@"Error: %@", error.description);
                                   }
                               }];
Run Code Online (Sandbox Code Playgroud)

如果您注意到我的第一个NSLog上面的评论,我可以打印出一些没有任何问题的东西.但是,如果我尝试打印出item.identifier的属性,应用程序将完全崩溃.崩溃日志是这样的:

  Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[GTLYouTubeVideo videoId]: unrecognized selector sent
Run Code Online (Sandbox Code Playgroud)

现在,item.identifier是一个GTLYouTubeResourceId,为什么它认为我试图从GTLYoutubeVideo获取属性?

谢谢您的帮助!

小智 6

尝试直接访问标识符对象的JSON.那里似乎有一个"videoId"值.像这样:

[item.identifier.JSON objectForKey:@"videoId"]