问题
According to the docs here: https://www.parse.com/docs/ios_guide#files-progress/iOS
this is the suggested syntax to handle file saving with a completion block and progressBlock.
let str = "Working at Parse is great!"
let data = str.dataUsingEncoding(NSUTF8StringEncoding)
let file = PFFile(name:"resume.txt", data:data)
file.saveInBackgroundWithBlock {
(succeeded: Bool!, error: NSError!) -> Void in
// Handle success or failure here ...
}, progressBlock: {
(percentDone: Int) -> Void in
// Update your progress spinner here. percentDone will be between 0 and 100.
}
However, XCode 6.2 throws this error: Consecutive statements on a line must be separated by ';'
on this line:
}, progressBlock: {
Anyone know how to properly utilize the progressBlock in this scenario?
Edit 1: Here's the sample in Obj C:
NSData *data = [@"Working at Parse is great!" dataUsingEncoding:NSUTF8StringEncoding];
PFFile *file = [PFFile fileWithName:@"resume.txt" data:data];
[file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
// Handle success or failure here ...
} progressBlock:^(int percentDone) {
// Update your progress spinner here. percentDone will be between 0 and 100.
}];
Edit 2:
Another attempt, different error:
Edit 3: Original code, but with CInt per a comment suggestion
回答1:
I defined a class in Objective C with the method signature:
- (void)saveInBackgroundWithBlock:(void(^)(BOOL succeeded, NSError *error))block progressBlock:(void(^)(int percentDone))progressBlock;
I can call it like so from Swift:
let file = Test()
file.saveInBackgroundWithBlock({(success: Bool, error: NSError!) -> Void in
NSLog("1")
}, progressBlock: { (percentage: CInt) -> Void in
NSLog("2")
})
回答2:
You are missing () around method arguments. Should be:
file.saveInBackgroundWithBlock({ (succeeded: Bool!, error: NSError!) -> Void in
// Handle success or failure here ...
}, progressBlock: {
(percentDone: Int) -> Void in
// Update your progress spinner here. percentDone will be between 0 and 100.
})
(Note: when calling Objective-C from Swift code, Xcode translates (in code completion) int into CInt, and NSInteger into Int).
来源:https://stackoverflow.com/questions/24548837/swift-2-consecutive-closures-blocks