objective-c - 尝试使用 objective-c 中的 youtube api 获取 channelId 时出现异常

标签 objective-c youtube-api

我正在尝试使用 google-api-objectivec-client 获取 youtube channel ID。我遇到的问题基本上是由于某种原因我在尝试访问 channelId 时收到异常。我使用的代码:

GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
service.APIKey = _MY_API_KEY_;
GTLQueryYouTube *query = [GTLQueryYouTube queryForSearchListWithPart:@"id"];
query.q = @"google";
query.type = @"channel";
query.maxResults = 1;
GTLServiceTicket *ticket = [service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
    if (error == nil) {
        GTLYouTubeSearchListResponse *products = object;
        for (id item in products.items) {
            GTLYouTubeSearchResult *result = item;
            NSLog(@"Identifier:%@",result.identifier);
            GTLYouTubeResourceId* resourceId = result.identifier;
            NSLog(@"kind:%@",resourceId.kind);
            NSLog(@"channel:%@",resourceId.channelId);
        }
    }else{
        NSLog(@"Error: %@", error.description);
    }
}];

当我运行这段代码时得到的输出是:

2013-04-05 11:37:12.615 YouTest[21704:11303] Identifier:GTLYouTubeChannel 0x7233b00: {kind:"youtube#channel" channelId?:"UCK8sQmJBp8GCxrOtXWBpyEA"}
2013-04-05 11:37:12.617 YouTest[21704:11303] kind:youtube#channel
2013-04-05 11:37:12.617 YouTest[21704:11303] -[GTLYouTubeChannel channelId]: unrecognized selector sent to instance 0x7233b00
2013-04-05 11:37:12.618 YouTest[21704:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[GTLYouTubeChannel channelId]: unrecognized selector sent to instance 0x7233b00'

所以我的实现在我尝试访问 resourceId 的 channelId 时崩溃了。从文档中我了解到 channelId 应该存在,因为 resourceId 的类型是 youtube#channel。 channelId 可以从我也在打印的 result.identifier 字符串中解析出来,但由于 channelId 有一个属性,我更愿意使用它。

关于我的代码有什么问题有什么想法吗?

最佳答案

Google 库中确实存在错误。然而,我通过直接访问 JSON 字符串并在 NSString+SBJSON.h 类的帮助下解析它来解决这个问题,如本例所示。

#import "NSString+SBJSON.h"

...

GTLYouTubeResourceId *resource = channel.snippet.resourceId;

NSDictionary *jsonObject = [resource.JSONString JSONValue];

NSString *channelid = [jsonObject valueForKey:@"channelId"];

关于objective-c - 尝试使用 objective-c 中的 youtube api 获取 channelId 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15829706/

相关文章:

objective-c - Swift 中 NSCoding 协议(protocol)的实现有什么问题

iphone - 触摸移动时将底 View 移至顶 View

iphone - DejalActivityView 和指标 View 错误

php - 使用 Youtube API 检索基本数据

ios - UIAlertviewController 在 IOS8.3 中崩溃

ios - MPMediaItem 不使用 MPMediaItemPropertyAssetURL 在 AVAudioPlayer 中播放

python - 使用 Python 将 youtube 视频添加到播放列表

javascript - 具有主干的 Youtube IFrame API

javascript - 使用 YouTube api 时,除非我先提醒视频 ID,否则 loadvideo 函数将无法工作

php - 我们如何使用 youtube api 获取未列出的播放列表