2016-09-28 96 views
0
func downloadProgress(bytesRead: Int64, totalBytesRead: Int64, 
    totalBytesExpectedToRead: Int64) { 
    let percent = Float(totalBytesRead)/Float(totalBytesExpectedToRead) 

    dispatch_async(dispatch_get_main_queue(), { 
     self.progress.setProgress(percent,animated:true) 
    }) 
    print("Progress:\(percent*100)%") 
} 

func downloadResponse(request: NSURLRequest?, response: NSHTTPURLResponse?, 
    data: NSData?, error:NSError?) { 
    if let error = error { 
     if error.code == NSURLErrorCancelled { 
      self.cancelledData = data 
     } else { 
      print("Failed to download file: \(response) \(error)") 
     } 
    } else { 
     print("Successfully downloaded file: \(response)") 
    } 
} 

@IBAction func continueBtnClick(sender: AnyObject) { 
    if let cancelledData = self.cancelledData { 
     self.downloadRequest = Alamofire.download(resumeData: cancelledData, 
      destination: destination) 

     self.downloadRequest.progress(downloadProgress) 

     self.downloadRequest.response(completionHandler: downloadResponse) 

     self.stopBtn.enabled = true 
     self.continueBtn.enabled = false 
    } 
} 

這些代碼在Alamofire 3.1上運行良好,但在升級到Swift 3.0和Alamofire 4.0後拒絕工作。Alamofire方法無法升級到4.0後工作

下面的兩個行顯示錯誤 「沒有這樣的memeber FO進展」 和

  1. self.downloadRequest.progress(downloadProgress)
  2. self.downloadRequest.response(completionHandler 「響應沒有這樣的成員」: downloadResponse)

我該如何解決這兩個問題?

謝謝。

回答

0

您所指的功能已隨Alamofire 4.0更改。 「無成員」錯誤的原因是函數調用已更改。根據新的文件,這是你應該(可能)來進行調用:

Alamofire.download("https://httpbin.org/image/png") 
.downloadProgress { progress in 
    print("Download Progress: \(progress.fractionCompleted)") 
} 
.responseData { response in 
    if let data = response.result.value { 
    } 
} 

根據Xcode中的新功能(我用Alamofire 4.0以及但不與。下載):

downloadRequest.downloadProgress(closure: Request.ProgressHandler) 
downloadRequest.responseData(completionHandler: (DownloadResponse<Data>) -> Void) 

來源:Alamofire documentation for download progress

+0

謝謝你的建議。有用。 – jdleung

+0

@jdleung太棒了!我很高興能夠幫忙 – tech4242

相關問題