Shubham Sharma
Shubham Sharma

Reputation: 529

AFJSONRequestOperation : variable 'operation' is uninitialized when captured by block

I am getting the above warning in this code on line number 6 and the userInfo also becoming nil in the block.Please suggest something to remove this warning and userInfo issue.

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"some url"]];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:path parameters:parameters];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary* JSON)
                                     {
                                         [self.delegate didReceivedResponse:JSON withUserInfo:operation.userInfo];                                             
                                     } failure:^(NSURLRequest *request, NSHTTPURLResponse *response,
                                                 NSError *error, id JSON)
                                     {
                                         NSLog(@"Network : %@",error);
                                         [self.delegate didFailWithError:error withUserInfo:operation.userInfo];
                                     }];
operation.userInfo = userInfo;
[operation start];

Upvotes: 2

Views: 1383

Answers (1)

Grzegorz Krukowski
Grzegorz Krukowski

Reputation: 19802

Just use userInfo variable when you create operation, instead of calling operation.userInfo - which is not existing in a moment you are creating it.

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"some url"]];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:path parameters:parameters];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary* JSON)
{
    [self.delegate didReceivedResponse:JSON withUserInfo:userInfo];                                             
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response,
    NSError *error, id JSON)
{
    NSLog(@"Network : %@",error);
    [self.delegate didFailWithError:error withUserInfo:userInfo];
}];
operation.userInfo = userInfo;
[operation start];

Another option is to set block later:

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"some url"]];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:path parameters:parameters];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:nil failure:nil];
operation.userInfo = userInfo;

[operation setCompletionBlockWithSuccess:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary* JSON)
{
    [self.delegate didReceivedResponse:JSON withUserInfo:userInfo];                                             
 } failure:^(NSURLRequest *request, NSHTTPURLResponse *response,
                                                 NSError *error, id JSON)
 {
     NSLog(@"Network : %@",error);
     [self.delegate didFailWithError:error withUserInfo:userInfo];
 }];

[operation start];

Upvotes: 1

Related Questions