gpt4 book ai didi

ios - 来自iOS应用中youtube播放列表的URL

转载 作者:行者123 更新时间:2023-12-01 16:37:41 25 4
gpt4 key购买 nike

我正在上一个类,该类使用YouTube Objective-C API检索youtube播放列表的链接。

每个URL作为字符串strURL存储在称为arrayURL的数组中(目前)arrayURL最终存储在arrayFinal中,我想从另一个类访问它。

在函数中,我得到了想要的信息(NSLog(@"%@", self.arrayFinal);)。但是当从另一个类(例如ViewController)调用函数时,我总是会得到一个空数组。

ViewController.m
YTDataHandler *youtubeObj = [YTDataHandler new];
[youtubeObj initYoutubeArray];
NSLog(@"%@", [youtubeObj arrayFinal]);

YTDataHandler.h

#import <Foundation/Foundation.h>
@interface YTDataHandler : NSObject
{
NSMutableArray *arrayFinal;
}

- (void)initYoutubeArray;
- (void)getYoutubeContent;

@property (nonatomic, retain) NSMutableArray *arrayFinal;

@end

YTDataHandler.m
#import "YTDataHandler.h"
#import "GTLServiceYouTube.h"
#import "GTLYouTube.h"
#import "GTLYouTubePlaylistSnippet.h"
#import "GTLYouTubeResourceId.h"

@implementation YTDataHandler

@synthesize arrayFinal;

- (void)initYoutubeArray {
self.arrayFinal = [[NSMutableArray alloc] init];

[self getYoutubeContent];
}

- (void)getYoutubeContent {
NSMutableArray *arrayURL = [[NSMutableArray alloc] init];

// Create a service object for executing queries
GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];

// API key
service.APIKey = @"YOUR_API_KEY";

// Create a query
GTLQueryYouTube *query = [GTLQueryYouTube queryForPlaylistItemsListWithPart : @"id, snippet, contentDetails"];
query.playlistId = @"UUa0XHGDbBL8re8UgO-OWNPA";
query.maxResults = 20;
query.type = @"video";

// 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) {

GTLYouTubePlaylistItemListResponse *items = object;

// iteration of items and subscript access to items.

for (GTLYouTubePlaylistItem *item in items) {

// IDs of videos
NSString *strVideoId = [item.snippet.resourceId JSONValueForKey : @"videoId"];


// encode and extend the videoId, result as an URL
NSString *strEncoded = [strVideoId stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding];
NSString *strURL = [NSString stringWithFormat:@"http://www.youtube.com/watch?v=%@", strEncoded];

// store strURL
[arrayURL addObject : (@"%@", strURL)];

}
// finally store arrayURL
[self.arrayFinal addObject : (@"%@", arrayURL)];
NSLog(@"%@", self.arrayFinal);

} else {
NSLog(@"Error: %@", error);
}
}];
}


@end

我在 // Execute the query -part中做错了什么?

最佳答案

在 View Controller 中,当您在 YTDataHandler 实例上调用 getYoutubeContent 时,您不能期望立即填充该数组。最终将在执行查询和执行处理程序代码时填充。在您的处理程序代码中,它始终有效,因为您位于同一线程中,并且在准备就绪时使用arrayFinal。在 View Controller 中,您需要等到阵列准备好后才能开始使用它。例如。您可以在YTDataHandler上设置一个 bool(boolean) 标志,您的 View Controller 可以在该标志上注册一个键值观察回调(KVO),以便在阵列准备好时自动得到通知。

ps。
为什么不使用:

[arrayURL addObject: strURL];

代替:
[arrayURL addObject : (@"%@", strURL)];

关于ios - 来自iOS应用中youtube播放列表的URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26691264/

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