Objective c 在方法的最后一行之前首先执行块

Objective c 在方法的最后一行之前首先执行块,objective-c,block,Objective C,Block,目标c中块内的代码行在以后以相同方法执行其他代码行之后执行。 我的问题是: 有一个方法名为: -(NSDictionary*)callingWeatherService{ NSString *urlString = @"http://api.openweathermap.org/data/2.5/weather?q=London,uk"; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL UR

目标c中块内的代码行在以后以相同方法执行其他代码行之后执行。 我的问题是:

有一个方法名为:

-(NSDictionary*)callingWeatherService{
        NSString *urlString =  @"http://api.openweathermap.org/data/2.5/weather?q=London,uk";
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];

        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        operation.responseSerializer = [AFJSONResponseSerializer serializer];

        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

            // 3
            self.dictionary = (NSDictionary *)responseObject;


        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

            // 4
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                                message:[error localizedDescription]
                                                               delegate:nil
                                                      cancelButtonTitle:@"Ok"
                                                      otherButtonTitles:nil];
            [alertView show];
        }];


        // 5
        [operation start];
        return self.dictionary;
}
但是语句
返回self.dictionary在块执行之前首先执行,所以字典返回为
nil
; 语句
返回self.dictionary是否有解决方案仅在块执行后执行。我需要这种方法是为了一个特定的目的。

我不想使用委托,我已经在网络块中使用委托来获取字典数据

简短的回答是“否”-块设计为异步执行
,这意味着您永远不知道它们何时完成。改变这种行为真的很愚蠢


使用委托,或使用分配给字典变量的
KVO
NSNotifications
。如果我是你,我肯定会使用
委托

可能重复的可能重复的“块设计为异步执行”不一定。块只是可运行的东西,比如函数。它们可以同步运行,也可以异步运行,等等。