从块内部获取变量 [英] Getting variable from the inside of block

查看:84
本文介绍了从块内部获取变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AFNetworking 2从服务器获取数据,我需要取回responseObject,但是无论我做什么我仍然会得到<null>.

I'm using AFNetworking 2 to GET data from server and I need to get back the responseObject but no matter what I do i still get <null>.

这是向服务器发送GET请求的方法,作为响应,它获取了NSDictionary我想在另一种方法中使用的方法...

Here is the method which is sending GET request to server and in response it gets NSDictionary which I want to use in another method...

- (void)getCurrentVersionsForTimetableWithID:(NSString *)timetableID
{
    [[AFHTTPRequestOperationManager manager] GET:[NSString stringWithFormat:VERSIONS, timetableID] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // Here I want to get responseObject
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Couldn't get current versions: %@", [error localizedDescription]);
    }];
}

如果我调用此方法,则一切正常.但是当我尝试使其返回NSDictionary时:

If I call this method everything works fine. But when I try to make it return NSDictionary like this:

- (NSDictionary *)getCurrentVersionsForTimetableWithID:(NSString *)timetableID
{
    __block NSDictionary *currentVersions;

    [[AFHTTPRequestOperationManager manager] GET:[NSString stringWithFormat:VERSIONS, timetableID] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        currentVersions = responseObject;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Couldn't get current versions: %@", [error localizedDescription]);
    }];

    return currentVersions;
}

我得到<null>值.我知道这是由于异步而发生的,但是如何解决呢?我试图将另一个完成块传递给该方法,但是当我在另一个方法中调用它时,我仍然无法将结果分配给该变量……请大家帮帮我!

I get <null> value. I know this is happening because of async but how to solve this? I've tried to pass another completion block to this method but when I call it inside another one I still cannot assign the result to the variable... Please guys, help me!

推荐答案

您要传入以NSDictionary为参数的完成块:

You want to pass in a completion block that takes NSDictionary as parameter:

- (void)getCurrentVersionsForTimetableWithID:(NSString *)timetableID completion:(void (^)(NSDictionary* currentVersions))completion
{
    [[AFHTTPRequestOperationManager manager] GET:[NSString stringWithFormat:VERSIONS, timetableID] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        currentVersions = responseObject;

        if(completion){
            completion(currentVersions);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Couldn't get current versions: %@", [error localizedDescription]);
    }];
}

要使用它:

[self getCurrentVersionsForTimetableWithID:@"someId" 
                                completion:^(NSDictionary* currentVersions){
                                    // Do something with currentVersions
                                }];

这篇关于从块内部获取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆