gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 20:19:35 28 4
gpt4 key购买 nike

我正在尝试使用 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com