我想在成功块之外使用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。
有人能帮助我吗?