如何使用AFHTTPRequestOperation的响应对象?

时间:2015-09-11 12:02:04

标签: ios objective-c objective-c-blocks

我想在成功块之外使用AFHTTPRequestOperation的响应对象。以下是我的代码:

NSMutableArray *arr = [[NSMutableArray alloc] init];
NSURL *URL = [NSURL URLWithString:@"my_url"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    NSURLCredential *credential = [NSURLCredential credentialWithUser:@"user" password:@"pass" persistence:NSURLCredentialPersistenceNone];

    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [op setCredential:credential];
    op.responseSerializer = [AFJSONResponseSerializer serializer];
    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSLog(@"JSON: %@", responseObject);
        for(NSDictionary *item in responseObject){
          [arr addObject:item];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    [[NSOperationQueue mainQueue] addOperation:op];
    NSLog(@"%@",arr);// here arr getting null or empty

我得到了响应对象,但是如果我想在成功块之外使用responseObject,那么对象就会变为null。

有人能帮助我吗?

0 个答案:

没有答案
相关问题