【问题标题】:Alamofire method not work after upgrade to 4.0升级到 4.0 后 Alamofire 方法不起作用
【发布时间】:2016-09-28 06:18:18
【问题描述】:
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 后无法运行。

以下两行显示错误“no such memeber fo progress”和“no such member of response”

  1. self.downloadRequest.progress(downloadProgress)
  2. self.downloadRequest.response(completionHandler: downloadResponse)

如何解决这两个问题?

谢谢。

【问题讨论】:

    标签: ios swift alamofire swift3


    【解决方案1】:

    您所指的功能在 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,但不使用 .Download):

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

    来源:Alamofire documentation for download progress

    【讨论】:

    • @jdleung 太棒了!我很高兴能帮上忙
    猜你喜欢
    • 2023-03-16
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 2017-07-06
    • 2017-06-30
    • 1970-01-01
    • 2015-07-05
    相关资源
    最近更新 更多