这是我的代码:
// 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 获取属性?
谢谢您的帮助!