我想使用 AFNetworking 返回块内的数据



我有一个使用 AFJSONRequestOperation 的函数,我希望只有在成功后才返回结果。你能指出我正确的方向吗?我仍然对块和AFNetworking有点无知。

如果你发布了你的代码会更好

使用__block您可以在块内使用变量

__block NSString *msg;
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [hud hide:YES];
        NSLog(@"Success %@",  operation.responseString);
        NSDictionary *message = [NSJSONSerialization JSONObjectWithData:[operation.responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",message);
        msg = message
        ALERT(@"Posted", message[@"message"]);
    }
                                      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                          NSLog(@"error: %@",  operation.responseString);
                                          NSLog(@"%@",error);
                                      }];
    [operation start];

最新更新