gpt4 book ai didi

ios - 在 AFNetworking 3.0 失败 block 上获取 responseObject

转载 作者:IT王子 更新时间:2023-10-29 07:55:18 24 4
gpt4 key购买 nike

如何从 AFNetworking 3.x 中的失败 block 获取响应字符串,

在 2.x 版本中,这样做的方式是:

[manager GET:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *dictionary_FetchResult = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSDictionary *dictionary_FetchResult = operation.responseObject;
}];

但是在3.x版本中,返回 block 的参数没有操作,如下所示:

[manager POST:path parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *dictionary_FetchResult = responseObject;
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"Error: %@", error);
}];

所以我希望有人能够做到这一点。

最佳答案

只需在您的失败 block 中执行此操作:-

 NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
NSLog(@"%@",errResponse);

对于 Swift:-

var errResponse: String = String(data: (error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] as! NSData), encoding: NSUTF8StringEncoding)
NSLog("%@", errResponse)

针对 Swift 4.1 进行了更新

var errResponse: String = String(data: (error._userInfo![AFNetworkingOperationFailingURLResponseDataErrorKey] as! Data), encoding: String.Encoding.utf8)!
print(errResponse)

关于ios - 在 AFNetworking 3.0 失败 block 上获取 responseObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35083273/

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