我发现了这个非常有趣的方法(Parse : is it possible to follow progress of PFObject upload),我试图在swift扩展中转换objective-c类别。但我对值类型unsigned long
进行了过度训练
请查看以下代码 - 它会抛出异常(行:let progress:Int32 = Int32(100*count/numberOfCyclesRequired)
):致命错误:浮点值无法转换为Int32,因为它是无限或NaN。
我也不确定如何在swift中处理__block
前缀,计数更改也将在块外发生。
extension PFObject {
class func saveAllInBackground(objects: [AnyObject]!, chunkSize:Int, block: PFBooleanResultBlock!, progressBlock:PFProgressBlock) {
let numberOfCyclesRequired:Double = Double(objects.count / chunkSize)
var count:Double = 0
PFObject.saveAllInBackground(objects, chunkSize: chunkSize, block: block) { (trig:Bool) -> Void in
count++
let progress:Int32 = Int32(100*count/numberOfCyclesRequired)
progressBlock(progress)
}
}
class func saveAllInBackground(objects: [AnyObject]!, chunkSize:Int, block: PFBooleanResultBlock!, trigger:(Bool) -> Void) {
let range = NSMakeRange(0, objects.count <= chunkSize ? objects.count:chunkSize)
var saveArray:NSArray = (objects as NSArray).subarrayWithRange(range)
var nextArray:NSArray = []
if range.length < objects.count {
nextArray = (objects as NSArray).subarrayWithRange(NSMakeRange(range.length, objects.count-range.length))
}
PFObject.saveAllInBackground(saveArray) { (succeeded:Bool, error: NSError!) -> Void in
if (error == nil && succeeded && nextArray.count != 0) {
trigger(true)
PFObject.saveAllInBackground(nextArray, chunkSize: chunkSize, block: block, trigger: trigger)
} else {
trigger(true)
block(succeeded,error)
}
}
}
}
提前感谢您的帮助。