2017-06-01 146 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)