gpt4 book ai didi

ios - 从 Twitter 帐户 block 返回 nsmutablearray

转载 作者:行者123 更新时间:2023-11-29 03:50:42 25 4
gpt4 key购买 nike

我正在尝试创建一个返回 twitter 用户列表的函数,但我无法使该函数返回 nsmutablearray。这是我执行的代码

-(NSMutableArray*) getTwitterUsersInPosition: (Position*) position

{ //请求访问 Twitter 帐户

ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error){
if (granted) {

NSArray *accounts = [accountStore accountsWithAccountType:accountType];

// Check if the users has setup at least one Twitter account

if (accounts.count > 0)
{
ACAccount *twitterAccount = [accounts objectAtIndex:0];

// Creating a request to get the info about a user on Twitter

NSArray *keys = [NSArray arrayWithObjects:@"geocode", @"count", nil];
NSArray *objects = [NSArray arrayWithObjects:@"37.781157,-122.398720,100mi", @"2", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects
forKeys:keys];

SLRequest *twitterInfoRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/search/tweets.json"] parameters:dictionary];
[twitterInfoRequest setAccount:twitterAccount];

// Making the request

[twitterInfoRequest performRequestWithHandler: ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{

// Check if we reached the rate limit

if ([urlResponse statusCode] == 429) {
NSLog(@"Rate limit reached");
return;
}

// Check if there was an error

if (error) {
NSLog(@"Error: %@", error.localizedDescription);
return;
}

// Check if there is some response data

if (responseData) {

NSError *error = nil;
NSArray *TWData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];

NSMutableArray *usersArray= [[NSMutableArray alloc] init];

NSMutableArray* statuses= [(NSDictionary *) TWData objectForKey:@"statuses"];

for (NSDictionary* dict in statuses )
{
NSString* user = [[dict objectForKey:@"user"] objectForKey:@"screen_name"];
NSLog(@"%@",user);
[usersArray addObject:user];
}
return userArray; //Here it says "Return type 'NSMutabelArray *' must match previous type 'void' when block literal has unspecified return type


}

});


}];
}
} else {
NSLog(@"No access granted");
}
}];

感谢您的帮助。

最佳答案

您的方法不能有返回类型,因为您想要返回的值是异步获取的,并且在方法完成后才可用。

您需要重写您的方法以使其返回数据:

  1. 致代表
  2. 通过街区
  3. 进入实例变量并触发一些更新
  4. 通过通知

基本上是一些允许您处理异步响应的机制。请不要使调用同步。

关于ios - 从 Twitter 帐户 block 返回 nsmutablearray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17113888/

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