Swift:2 个连续的闭包/块 [英] Swift: 2 consecutive closures/blocks
问题描述
根据此处的文档: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.
}
然而,XCode 6.2 抛出这个错误:一行中的连续语句必须用';'分隔
However, XCode 6.2 throws this error: Consecutive statements on a line must be separated by ';'
在这一行:
}, progressBlock: {
有谁知道在这种情况下如何正确使用progressBlock?
Anyone know how to properly utilize the progressBlock in this scenario?
这是 Obj C 中的示例:
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.
}];
编辑 2:
另一次尝试,不同的错误:
Another attempt, different error:
编辑 3:原始代码,但根据评论建议使用 CInt
Edit 3: Original code, but with CInt per a comment suggestion
推荐答案
我在 Objective C 中定义了一个带有方法签名的类:
I defined a class in Objective C with the method signature:
- (void)saveInBackgroundWithBlock:(void(^)(BOOL succeeded, NSError *error))block progressBlock:(void(^)(int percentDone))progressBlock;
我可以在 Swift 中这样称呼它:
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")
})
这篇关于Swift:2 个连续的闭包/块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!