AFHTTPSessionManager does not pass JSON error responses to failure block
以往採用AFNetworking 2.x呼叫API後,無論在success或failure的block,都可以取得http response資訊,從而依需求取得所需的資訊,像是API執行失敗後回拋的說明資料等等
但大升級到AFNetworking 3.x後,發現failure block不再提供response相關資訊,其實是全部統一包進NSError了,二種版本的差異如下
AFNetworking 2.x
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation ,id JSON) {
// do something if success
} failure:^(AFHTTPRequestOperation *operation , NSError *error) {
// do something if failure
NSData *data = [operation.responseString dataUsingEncoding:NSUTF8StringEncoding];
id dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
[self showJWTError:[dict objectForKey:@"error_description"]];
}];
AFNetworking 3.x
[manager POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// do something if success
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// do something if failure
NSData *data = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];
NSDictionary *serializedData = [NSJSONSerialization JSONObjectWithData: data options:kNilOptions error:nil];
[self showJWTError:[serializedData objectForKey:@"error_description"]];
}];
ps: showJWTError是我用來呈現錯誤資訊給用戶的自訂方法
Logical not is only applied to the left hand side of this comparison
若左側運算元 (left hand side operand) 旁邊有 Logical Not ,又沒有用括號包起來就會出現Logical not is only applied to the left hand side of this comparison警告
例如
if(![self.someParameter caseInsensitiveCompare:@"Group"] == NSOrderedSame)
if(!([self.someParameter caseInsensitiveCompare:@"Group"] == NSOrderedSame))
或是
if((![self.someParameter caseInsensitiveCompare:@"Group"]) == NSOrderedSame)
訂閱:
文章 (Atom)