当我尝试在我的 block 上实现进度计时器时,我无法准确地意识到为什么我会收到问题标题中引用的错误。
func uploadImage(imageData: NSData!) {
let imageFile = PFFile(name: "\(PFUser.currentUser().username)'s Avatar", data: imageData)
// Show the HUD to prompt user of upload
hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
hud.mode = MBProgressHUDModeDeterminateHorizontalBar
hud.labelText = "Changing Image..."
// Upload the image
imageFile.saveInBackgroundWithBlock({ (succeeded: Bool, error: NSError!) -> Void in
// Check there was no error, begin handling the file upload
// trimmed out un-necessary code
}, progressBlock: { (amountDone: Int32) -> Void in
self.hud.progress = amountDone/100 as Float
})
}
如果我删除行 self.hud.progress = amountDone/100 as Float
一切都会按我的预期工作,但是将任何内容放在 progressBlock
中会导致我的应用程序崩溃。
我正在关注Parse tutorial for uploading images ,唯一的区别是我正在使用 Swift,所以我需要从 Objective-C 进行转换,但是我不明白为什么这会对构造第二个 block 的语法产生巨大的差异。
最佳答案
这是类型转换问题。
您需要将 amountDone
变量从 Int32
转换为 Float
或 CGFloat
。
var flotAmount = Float(amountDone)
检查 self.hud.progress
的类型,如果它是 Float
则使用
self.hud.progress = Float(flotAmount/100)
如果是 CGFloat 则使用下面一行:
self.hud.progress = CGFloat(flotAmount/100)
关于ios - Swift:调用中的额外参数 "progressblock",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28090015/