AFJSONRequestOperation:变量“操作”在被块捕获时未初始化
Posted
技术标签:
【中文标题】AFJSONRequestOperation:变量“操作”在被块捕获时未初始化【英文标题】:AFJSONRequestOperation : variable 'operation' is uninitialized when captured by block 【发布时间】:2013-12-17 09:06:00 【问题描述】:我在第 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];
【问题讨论】:
【参考方案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];
【讨论】:
它不起作用,因为操作时没有successBlock这样的属性。 它现在工作正常,没有任何警告。非常感谢。 AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:nil failure:nil]; 抱歉出现错误 - 是在没有编译器的情况下从头顶写的。我按照建议修复了,谢谢。以上是关于AFJSONRequestOperation:变量“操作”在被块捕获时未初始化的主要内容,如果未能解决你的问题,请参考以下文章
如何从 AFNetworking 和 AFJSONRequestOperation 获得可变字典?
使用 AFJSONRequestOperation 返回 JSON