gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:37:53 24 4
gpt4 key购买 nike

我在第 6 行的这段代码中收到上述警告,并且 userInfo 在该 block 中也变为 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];

最佳答案

创建操作时只需使用 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];

关于ios - AFJSONRequestOperation : variable 'operation' is uninitialized when captured by block,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20629931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com