Swift: 2个连续的闭包/块

4
根据这里的文档:https://www.parse.com/docs/ios_guide#files-progress/iOS,建议使用以下语法来处理带有完成块和进度块的文件保存。
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.
}

然而,XCode 6.2 抛出了这个错误: 一行上连续的语句必须用 ";" 分隔。 在这一行上:
}, progressBlock: {

有人知道如何在这种情况下正确使用progressBlock吗?

编辑1: 以下是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.
}];

编辑2:

另一次尝试,出现了不同的错误:

另一次尝试,又一个错误

编辑3:

原始代码,但根据评论建议添加了CInt:

输入图像描述


你指的Xcode 6.2是Xcode 6 beta 2吗? - ThomasW
2个回答

1
我将在 Objective-C 中定义一个类,并使用以下方法签名:

- (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")
    })

“block:”可能是不必要的,因为它是第一个参数。 - Léo Natan
"调用中存在额外的参数'block'" - Z Jones
尝试不使用“block:”进行操作。 - Léo Natan
"调用中有多余的参数" - Z Jones
1
是的。唯一的区别是我使用了“CInt”作为进度百分比的参数。 - Léo Natan
显示剩余3条评论

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

(注意:当从Swift代码调用Objective-C时,Xcode将int翻译为CInt,将NSInteger翻译为Int。)

调用中存在多余的参数'progressBlock' - Bagusflyer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接