Swift:连续2次关闭/阻止

时间:2014-07-03 08:37:11

标签: xcode swift xcode6

根据此处的文档:https://www.parse.com/docs/ios_guide#files-progress/iOS

这是使用完成块和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.
}

但是,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:

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

Another attempt, another error

编辑3: 原始代码,但根据评论建议使用CInt

enter image description here

2 个答案:

答案 0 :(得分: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")
    })

答案 1 :(得分: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)。