2015-04-23 51 views
1

我試圖實現雨燕下面的代碼的PUT版本(這樣我就可以實現一個PUT附件),網址爲:https://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking/AFHTTPSessionManager.m翻譯AFNetworking POST constructingBodyWithBlock從目標C斯威夫特

我有麻煩讓它編譯,它說變量taskused within its own initial value。有什麼想法嗎?

目標C

- (NSURLSessionDataTask *)POST:(NSString *)URLString 
        parameters:(id)parameters 
    constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block 
         success:(void (^)(NSURLSessionDataTask *task, id responseObject))success 
         failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure 
{ 
    NSError *serializationError = nil; 
    NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block error:&serializationError]; 
    if (serializationError) { 
     if (failure) { 
#pragma clang diagnostic push 
#pragma clang diagnostic ignored "-Wgnu" 
      dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{ 
       failure(nil, serializationError); 
      }); 
#pragma clang diagnostic pop 
     } 

     return nil; 
    } 

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

    [task resume]; 

    return task; 
} 

斯威夫特

func PUT(URLString: String!, parameters: AnyObject!, constructingBodyWithBlock block: ((AFMultipartFormData!) -> Void)!, 
    success: ((NSURLSessionDataTask!, AnyObject!) -> Void)!, 
    failure: ((NSURLSessionDataTask!, NSError!) -> Void)!) -> NSURLSessionDataTask! { 

    let urlX = "http://localhost/\(URLString)" //still need to convert to baseURL 

    var serializationError:NSErrorPointer = nil 

    let request:NSMutableURLRequest = self.requestSerializer.multipartFormRequestWithMethod("PUT", URLString: urlX, parameters: parameters as! [NSObject : AnyObject]!, constructingBodyWithBlock: block, error: serializationError) 

    let task:NSURLSessionDataTask = self.uploadTaskWithStreamedRequest(request, progress: nil, completionHandler: { (response: NSURLResponse!, responseObject: AnyObject!, error: NSError!) -> Void in 
     if ((error) != nil) { 
      if ((failure) != nil) { 
       failure(task, error) <-- error 
      } 
     } else { 
      if ((success) != nil) { 
       success(task, responsObject) <-- error 
      } 
     } 
    }) 

回答

0

更新的響應

你得到的錯誤是自己的初始值,這意味着內使用變你基本上是你在聲明之前唱出恆定的task,因爲您在聲明中使用它。

去解決這個問題的正確方法是將改寫uploadTaskWithStreamedRequest()返回類型的元組(AnyObject,NSError)。不幸的是,這涉及到更多的工作。我對你的問題的解決方案是申報uploadTaskWithStreamedRequest()之外的幾個變量,然後將其設置等於在瓶蓋內捕獲的值。之後,您可以根據兩個可選的綁定測試調用完成塊,其中只有一個會通過。

func PUT(URLString: String!, parameters: AnyObject!, constructingBodyWithBlock block: ((AFMultipartFormData!) -> Void)!, 
    success: ((NSURLSessionDataTask!, AnyObject!) -> Void)!, 
    failure: ((NSURLSessionDataTask!, NSError!) -> Void)!) -> NSURLSessionDataTask! { 

     let urlX = "http://localhost/\(URLString)" //still need to convert to baseURL 

     var serializationError:NSErrorPointer = nil 

     let request:NSMutableURLRequest = self.requestSerializer.multipartFormRequestWithMethod("PUT", URLString: urlX, parameters: parameters as! [NSObject : AnyObject]!, constructingBodyWithBlock: block, error: serializationError) 

     var outerError: NSError? 
     var outerResponseObject: AnyObject? 
     let task = self.uploadTaskWithStreamedRequest(request, progress: nil, completionHandler: { (response: NSURLResponse!, responseObject: AnyObject!, error: NSError!) -> Void in 
      if ((error) != nil) { 
       if ((failure) != nil) { 
        outerError = error 
       } 
      } else { 
       if ((success) != nil) { 
        outerResponseObject = responseObject 
       } 
      } 
     }) 

     if let outerError = outerError { 
      failure(task, outerError) 
     } 

     if let outerResponseObject: AnyObject = outerResponseObject { 
      success(task, outerResponseObject) 
     } 

     return task 
} 

原始響應

爲什麼甚至懶得這樣做,當AFNetworking帶有做出PUT請求的能力嗎?

https://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking/AFHTTPSessionManager.m#L186

- (NSURLSessionDataTask *)PUT:(NSString *)URLString 
        parameters:(id)parameters 
         success:(void (^)(NSURLSessionDataTask *task, id responseObject))success 
         failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure 
{ 
    NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"PUT" URLString:URLString parameters:parameters success:success failure:failure]; 

    [dataTask resume]; 

    return dataTask; 
} 
+0

我試圖實現與附件,類似與constructingBodyWithBlock爲POST PUT,但AFNetworking沒有它的PUT版本。 – Dean

+0

好的,夠公平的。讓我加載了一個項目,看看我找到:) – ArtSabintsev

+0

好吧,我覺得問題是,在斯威夫特你想使用它自己的定義中恆'task',因此誤差範圍內***變量用於其自己的初始值***。讓我再修補一下。 – ArtSabintsev