我在第6行的代码中收到了上述警告,并且userInfo在块中也变为nil。请建议删除此警告和userInfo问题。
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];
答案 0 :(得分:1)
在创建操作时使用userInfo变量,而不是调用operation.userInfo - 在创建它时不会存在。
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];
另一种选择是稍后设置块:
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];