0

这是我的代码:

  // 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);
                                   }
                               }];

如果您注意到我的第一个 NSLog 上方的评论,我可以毫无问题地打印出许多内容。但是,如果我尝试打印 item.identifier 的属性,应用程序将完全崩溃。崩溃日志是这样的:

  Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[GTLYouTubeVideo videoId]: unrecognized selector sent

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

谢谢您的帮助!

4

2 回答 2

6

Try accessing the JSON of the identifier object directly. There appears to be a "videoId" value in there. like this:

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

于 2013-04-23T00:48:13.440 回答
0

现在, item.identifier 是一个GTLYouTubeResourceId

显然,它不是。

那么为什么它认为我正在尝试从GTLYoutubeVideo???

Because you wrote three consecutive question marks. Because it's a GTLYoutubeVideo instance, in fact. Sorry, C (and thus Objective-C) is not a very type-safe language. You can declare objects to be of some type, and they can be assigned a pointer to some other kind of object at runtime.

Here, I suspect you either misread something in the documentation or there's a bug in the client library or this is a weird memory management error, but one thing is sure: the runtime is always right.

于 2013-03-30T18:16:47.687 回答