0

我正在尝试创建一个返回 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");
    }
}];

}

谢谢你的帮助。

4

2 回答 2

1

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

您需要重写您的方法,以便它返回数据:

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

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

于 2013-06-14T17:28:04.010 回答
0

在您的代码中间,您只需编写return不带值的代码(就好像您的方法的返回类型为void):

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

// Check if there was an error

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

将其更改为return nil,代码应该可以编译。

于 2013-06-14T17:23:54.093 回答