Swift: 2 consecutive closures / blocks

According to the docs here: https://www.parse.com/docs/ios_guide#files-progress/iOS

this is the suggested syntax for handling file save 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: Sequential statements in a line must be separated by a ';'

in this line:

}, progressBlock: {

Does anyone know how to use progressBlock correctly in this scenario?

Edit 1: Here is a 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, another error:

Another attempt, another error

Edit 3: Source code, but with CInt to suggest comments

enter image description here

+4
source share
2 answers

I defined a class in Objective-C with a method signature:

- (void)saveInBackgroundWithBlock:(void(^)(BOOL succeeded, NSError *error))block progressBlock:(void(^)(int percentDone))progressBlock;

Swift:

let file = Test()
file.saveInBackgroundWithBlock({(success: Bool, error: NSError!) -> Void in
        NSLog("1")
    }, progressBlock: { (percentage: CInt) -> Void in
        NSLog("2")
    })
+1

() . :

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.
})

(: Objective-C Swift Xcode ( ) int CInt NSInteger int).

+1

Source: https://habr.com/ru/post/1546966/


All Articles