我正在尝试从 Objective C 中“翻译”一种方法,因为我在 swift 方面做得更好,但我遇到了一个我无法解决的错误。
这是方法:
+(void) getFormObjectForWorkflowTypeId: (NSNumber *) workflowTypeId andEmployeeId: (NSNumber *) employeeId onCompletion: (void (^)(VOCForm *form)) completionHandler {
VOCGetFormRC *configuration = [[VOCGetFormRC alloc] initWithWorkflowTypeId: workflowTypeId andEmployeeId: employeeId];
VOCNetworkManager *networkManager = [VOCNetworkManager sharedManager];
[networkManager startRequestWithConfiguration: configuration
onSuccess:^(NSArray * _Nonnull jsonResponse) {
BOOL responseState = [jsonResponse valueForKey:@"success"];
if (responseState) {
NSDictionary *formData = [[[jsonResponse valueForKey: @"data"] valueForKey: @"elements"] valueForKey: @"form"];
NSError *error;
VOCForm *form = [[VOCForm alloc] initWithDictionary: formData error: &error];
if (error) {
[VOCLogUtils printLogForClass: self methodName: NSStringFromSelector(_cmd) andError: error];
if (completionHandler) {
completionHandler (nil);
}
}
else {
if (completionHandler) {
completionHandler (form);
}
}
}
else {
if (completionHandler) {
completionHandler(nil);
}
}
}
onFailure:^(VOCNetworkManagerResponse *networkManagerResponse) {
if (completionHandler) {
completionHandler(nil);
}
}];
}
当我尝试打电话时,问题就出现在开始时
networkManager.startRequest
Xcode 使用“onCompletion”而不是 onSuccess 自动完成方法。无论如何它都无法识别“jsonResponse”。
在 VOCNetworkManager 类中有两个同名的方法(我不知道这在Objective C中是否常见):
一个叫'onSucces'
-(void) startRequestWithConfiguration: (VOCRequestConfiguration *) configuration onSuccess: (void ( ^ _Nonnull)(NSArray * _Nonnull jsonResponse)) success
onFailure: (void (^)(VOCNetworkManagerResponse *networkManagerResponse)) failure {
//NSLog(@"STARTING REQUEST NETWORK MANAGER");
NSString *url = [NSString stringWithFormat: @"%@%@", configuration.baseUrl, configuration.endPoint];
第二个叫做“onCompletion”
-(void) startRequestWithConfiguration: (VOCRequestConfiguration *) configuration onCompletion: (void (^)(NSDictionary *jsonResponse, NSError *error)) completionHandler {
NSString *url = [NSString stringWithFormat: @"%@%@", configuration.baseUrl, configuration.endPoint];
[[self getNetworkingManager] POST: url parameters: configuration.parameters progress: nil
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
但是从我的 Swift 类看来,如果我只能得到第二个,所以我根本无法转换该方法。
这不是我第一次遇到 completionHandlers tbh 的问题
头文件
#import <Foundation/Foundation.h>
@class VOCRequestConfiguration;
@class VOCNetworkManagerResponse;
@class VOCMultipartRequestConfiguration;
@interface VOCNetworkManager : NSObject
+(id _Nullable) sharedManager;
-(void) startRequestWithConfiguration: (VOCRequestConfiguration *_Nullable) configuration onSuccess: (void ( ^ _Nonnull)( NSArray * _Nonnull jsonResponse)) success
onFailure: (void (^_Nullable)(VOCNetworkManagerResponse * _Nullable networkManagerResponse)) failure;
-(void) startApplicationJSONContentRequestWithConfiguration: (VOCRequestConfiguration *_Nullable) configuration onSuccess: (void ( ^ _Nonnull)( NSArray * _Nonnull jsonResponse)) success
onFailure: (void (^_Nullable)(VOCNetworkManagerResponse * _Nullable networkManagerResponse)) failure;
-(void) startRequestForFile: (VOCMultipartRequestConfiguration *_Nullable) configuration onSuccess: (void (^_Nullable)(NSArray * _Nullable response)) success onFailure: (void (^_Nullable)(void)) failure;
-(void) startRequestWithConfiguration: (VOCRequestConfiguration *_Nullable) configuration onCompletion: (void (^_Nullable)(NSDictionary * _Nullable jsonResponse, NSError * _Nullable error)) completionHandler;
-(void) uploadFile: (VOCMultipartRequestConfiguration *_Nullable) configuration onSuccess: (void (^_Nullable)(NSArray * _Nullable response)) success onFailure: (void (^_Nullable)(void)) failure;
@end
嘿,编译器似乎想知道所有信息以使这些方法可用于 swift。
所以代替
@class VOCRequestConfiguration;
@class VOCNetworkManagerResponse;
@class VOCMultipartRequestConfiguration;
用
#import "VOCRequestConfiguration.h"
#import "VOCNetworkManagerResponse.h"
#import "VOCMultipartRequestConfiguration.h"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句