我正在尝试显示我下载的文件的进度视图,当我打印进度时它是正确的但是当我在进度视图上实现它是完全错误的它是3000%这里是我的代码。我正在使用MHRadialProgressView
downloadTask.observe(.progress) { (snapshot) -> Void in
// Download reported progress
let percentComplete = 100 * Double(snapshot.progress!.completedUnitCount) / Double(snapshot.progress!.totalUnitCount)
print("percentComplete")
self.progressView.moveNext(currentprogress as NSNumber!)
// Update the progress indicator
}
当我使用MBProgressHUD时,这里是代码
downloadTask.observe(.progress) { (snapshot) -> Void in
// Download reported progress
let percentComplete = 100 * Float(snapshot.progress!.completedUnitCount)
/ Float(snapshot.progress!.totalUnitCount)
let hud = MBProgressHUD.showAdded(to: (self.navigationController?.view)!, animated: true)
// Set the determinate mode to show task progress.
hud.mode = MBProgressHUDMode.determinate
hud.label.text = NSLocalizedString("Loading...", comment: "HUD loading title")
// Set up NSProgress
let progressObject = Progress(totalUnitCount: 100)
hud.progressObject = progressObject
// Configure a cancel button.
DispatchQueue.global(qos: .default).async(execute: {() -> Void in
// Do something useful in the background and update the HUD periodically.
while progressObject.fractionCompleted < 1.0 {
progressObject.becomeCurrent(withPendingUnitCount: 1)
progressObject.resignCurrent()
usleep(useconds_t(percentComplete))
}
DispatchQueue.main.async(execute: {() -> Void in
hud.hide(animated: true)
})
})
}
我收到此错误 nanfatal错误:浮点值无法转换为UInt32,因为它是无限或NaN
答案 0 :(得分:1)
一些建议:
MBProgressHUD
,这样您就可以set a progress object and forget about it。