在 Swift 3 中使用 Alamofire 为 macOS 应用程序下载文件时,我试图显示进度。
这是我所拥有的:
func downloadFile() {
progressIndicator.isHidden = false
progressIndicator.minValue = 0
progressIndicator.maxValue = 10
progressIndicator.isIndeterminate = false
outputTextField.stringValue = ""
let mainURL = "https://somethinghere.org/macos/\(comboCellBox.stringValue)"
let destination = DownloadRequest.suggestedDownloadDestination(for: .downloadsDirectory)
Alamofire.download(mainURL, to: destination)
.downloadProgress(queue: DispatchQueue.global(qos: .utility)) { progress in
print("Download Progress: \(progress.fractionCompleted)")
// I want to show progress from .fractionCompleted to NSProgressIndicator here
}
.responseJSON { response in
debugPrint(response)
}
}
对于每个 .fractionCompleted,我希望 NSProgressIndicator 显示和动画下载进度。
我不知道从哪里开始,我想知道是否有人可以帮助我开始。我已阅读 Alamofire 文档,但找不到任何相关内容。
编辑:
输出:
print("Download Progress: \(progress.fractionCompleted)")
看起来像这样:
Download Progress: 0.00208984202190035
Download Progress: 0.198987043477936
Download Progress: 0.298296286511484
Download Progress: 0.397831087926645
Download Progress: 0.49933983672564
Download Progress: 0.598874638140801
Download Progress: 0.698409439555962
Download Progress: 0.797944240971122
Download Progress: 0.897479042386283
Download Progress: 0.997013843801444
Download Progress: 1.0
回答:
self.progressIndicator.doubleValue = progress.fractionCompleted