objective-c - 迁移到 swift - 完成处理程序的问题
问题描述
我正在尝试从 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"
推荐阅读
- android - 从安卓终端设置时间
- java - 通过数组传递不确定数量的 Int
- mule - 连接 2 个变量的值并在 mule 4 中创建一个新变量
- c# - 如何将一个数组对象中的值分配给另一个数组?
- php - 通过cron更改php版本
- database - 当我向数据库添加一个新字段时,它会覆盖我添加的最后一个字段
- javascript - 当显示将来可能更改的选择选项列表时,我应该在 UI 中对它们进行硬编码还是从我的数据库中获取?
- python - 如何从 Django 中的查询集中获取所有相关字段?
- python - Networkx 的有向图遍历
- javascript - Chrome devtools 无法模拟推送事件