2015-10-19 87 views
2

雖然我打算將來切換到Alamofire,但與此同時,我正在將我的AFHTTPSessionManager子類從objc轉換爲swift。我有一些問題轉換我的自定義dataTaskWithHTTPMethod方法。該函數將覆蓋AFHTTPSessionManager.dataTaskWithHTTPMethod,以便我可以向請求添加一些額外的頭並將響應主體傳遞到失敗完成關閉中。在swift中使用AFHTTPSessionManager

@interface WebAPIClient : AFHTTPSessionManager 

和實現功能:

- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method 
            URLString:(NSString *)URLString 
            csrfToken:(NSString *)csrfToken 
            parameters:(id)parameters 
            success:(void (^)(NSURLSessionDataTask *, id))success 
            failure:(void (^)(NSURLSessionDataTask *, NSError *, id))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, nil); 
     }); 
    } 

    return nil; 
    } 

    if (csrfToken!=nil) { 
    [request setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"]; 
    [request setValue:csrfToken forHTTPHeaderField:@"X-CSRFToken"]; 
    } 


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

    return dataTask; 
} 

我主要是有困難找出如何重寫功能的第一部分。在swift中處理NSError並將其傳遞給函數的行爲有所不同,我無法使用NSErrorPointer替換。

回答

1

這是怎麼了你的方法看起來像斯威夫特:

typealias DataTaskSuccessHandler = (dataTask: NSURLSessionDataTask?, responseObject: AnyObject?) ->() 
typealias DataTaskErrorHandler = (dataTask: NSURLSessionDataTask?, error: NSError?, responseObject: AnyObject?) ->() 

func dataTask(withHTTPMethod httpMethod: String, urlString: String, csfrToken: String?, parameters: AnyObject?, success: DataTaskSuccessHandler?, failure: DataTaskErrorHandler?) -> NSURLSessionDataTask? { 
    var dataTask: NSURLSessionDataTask? 
    var serializationError: NSError? 
    guard let absoluteURLString = NSURL(string: urlString, relativeToURL: baseURL)?.absoluteString else { return nil } 
    let request = requestSerializer.requestWithMethod(httpMethod, URLString: absoluteURLString, parameters: parameters, error: &serializationError) 
    if serializationError != nil { 
     if let failure = failure { 
      dispatch_async(completionQueue ?? dispatch_get_main_queue(), {() -> Void in 
       failure(dataTask: nil, error: serializationError, responseObject: nil) 
      }) 
     } 
     return nil 
    } 

    if let csfrToken = csfrToken { 
     request.setValue("XMLHttpRequest", forHTTPHeaderField: "X-Requested-With") 
     request.setValue(csfrToken, forHTTPHeaderField: "X-CSRFToken") 
    } 

    dataTask = dataTaskWithRequest(request, completionHandler: { (_, responseObject, error) -> Void in 
     if let error = error, let failure = failure { 
      failure(dataTask: dataTask, error: error, responseObject: responseObject) 
     } else if let success = success { 
      success(dataTask: dataTask, responseObject: responseObject) 
     } 
    }) 

    return dataTask 
} 

我不知道,如果當你調用所以我把它們定義爲選配你的函數的successfailure關閉是必需的。如果您從未在沒有成功和失敗處理程序的情況下調用該方法,則可以刪除這些可選項並在執行這些關閉時清除if let