迁移到快速-完成处理程序问题

时间:2019-03-13 13:00:33

标签: objective-c swift migration objective-c-blocks

我正在尝试从“目标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类看来,如果我只能得到第二个,那么我根本就无法转换该方法。

这不是我第一次遇到completedHandlers 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

1 个答案:

答案 0 :(得分:0)

嘿,似乎编译器希望了解所有信息,以便迅速使用这些方法。

所以不是

inventory_dir

使用

@class VOCRequestConfiguration;
@class VOCNetworkManagerResponse;
@class VOCMultipartRequestConfiguration;