2017-06-01 147 views
1
调用目标C代码时不工作

我从SWIFT 3以下代码调用AFNetworking代码完成处理程序AFNetworking

typealias StartSessionTaskHandler = (_ task: URLSessionDataTask) -> Void 
typealias RetrySessionTaskHandler = (_ newTask: URLSessionDataTask) -> Void 
typealias SuccessResponseHandler = (_ task: URLSessionDataTask, _ result: AnyObject) -> Void 
typealias FailureResponseHandler = (_ task: URLSessionDataTask?, _ error: NSError) -> Void 


    fileprivate func POST(_ service: AFHTTPSessionManager, URLString: String, parameters: AnyObject?, handleAuthorization: Bool, start: StartSessionTaskHandler?, retry: RetrySessionTaskHandler?, success:SuccessResponseHandler?, failure: FailureResponseHandler?) { 

    /* 
    First, prepare the call 
    */ 

    print("*^*^*") 
    print(parameters!) 

    initiateSessionTaskWithAuthorization(handleAuthorization, failure: failure) { 
     /* 
     The session task is successfully initiated. Do the actual API call 
     */ 
     let task = service.post(URLString, parameters: parameters, progress: nil, success: success as? ((URLSessionDataTask, Any) -> Void), 

           failure: { 

            (task: URLSessionDataTask?, error: Error) in 

            /* 
            Try to handle the failure automatically 
            */ 
            self.handleFailureForSessionTask(task, error: error as NSError, handleAuthorizationFailure: handleAuthorization, retry: retry, failure: failure, 

             newTaskRetrieval: { 

              /* 
              This closure creates a copy of the original API call, but with the external failure handler (to avoid potential loops). It is used if we need to retry the original API call 
              */ 
              return service.post(URLString, parameters: parameters as! [AnyObject], progress: nil, success: success as! ((URLSessionDataTask, Any) -> Void), failure: failure as! ((URLSessionDataTask?, Error) -> Void)?) 
            }) 
     }) 

     /* 
     The task is successfully started 
     */ 
     if let task = task {// 
      start?(task) 
     } 
    } 
} 

这将调用来自AFnetwroking POST功能被定义为这个

- (NSURLSessionDataTask *)POST:(NSString *)URLString 
       parameters:(id)parameters 
        progress:(void (^)(NSProgress * _Nonnull))uploadProgress 
        success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success 
        failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure 
    { 

    NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"POST" URLString:URLString parameters:parameters uploadProgress:uploadProgress downloadProgress:nil success:success failure:failure]; 

     [dataTask resume]; 

     return dataTask; 
    } 

此内部调用它被定义,它返回基于从服务 响应成功或失败的datataskwithhttpmethod如下

- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method 
            URLString:(NSString *)URLString 
            parameters:(id)parameters 
           uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress 
          downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress 
            success:(void (^)(NSURLSessionDataTask *, id))success 
            failure:(void (^)(NSURLSessionDataTask *, NSError *))failure 

    { 
    NSError *serializationError = nil; 
    NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError]; 
if (serializationError) { 
    if (failure) { 
     dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{ 
      failure(nil, serializationError); 
     }); 
    } 

    return nil; 
} 

__block NSURLSessionDataTask *dataTask = nil; 
dataTask = [self dataTaskWithRequest:request 
         uploadProgress:uploadProgress 
        downloadProgress:downloadProgress 
        completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) { 
    if (error) { 
     if (failure) { 
      failure(dataTask, error); 
     } 
    } else { 
     if (success) { 
      success(dataTask, responseObject);   } 
    } 
}]; 

return dataTask; 
} 

当我尝试返回成功块返回它显示为NULL,它无法返回到swift代码的回应。这不是从迅速3.任何帮助表示赞赏。谢谢。

+0

其实我没有在该功能中使用成功块。我在调用堆栈中的其他地方使用它。如果我不把它作为一个块包含在内,你认为它是否为空?我会试试 – user2122350

+0

你的建议奏效。谢谢你的一样 – user2122350

回答

0

方法initiateSessionTaskWithAuthorization需要一个完成处理程序,您必须将其传递到service.post(...success:HERE)