xcode Swift:2 个连续的闭包/块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24548837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift: 2 consecutive closures/blocks
提问by Z Jones
According to the docs here: https://www.parse.com/docs/ios_guide#files-progress/iOS
根据这里的文档: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 ';'
但是,XCode 6.2 抛出此错误:一行中的连续语句必须以“;”分隔
on this line:
在这一行:
}, progressBlock: {
Anyone know how to properly utilize the progressBlock in this scenario?
有人知道如何在这种情况下正确使用 progressBlock 吗?
Edit 1: Here's the sample in Obj C:
编辑 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.
}];
Edit 2:
编辑2:
Another attempt, different error:
另一个尝试,不同的错误:
Edit 3: Original code, but with CInt per a comment suggestion
编辑 3:原始代码,但根据评论建议使用 CInt
采纳答案by Leo Natan
I defined a class in Objective C with the method signature:
我在 Objective C 中定义了一个带有方法签名的类:
- (void)saveInBackgroundWithBlock:(void(^)(BOOL succeeded, NSError *error))block progressBlock:(void(^)(int percentDone))progressBlock;
I can call it like so from Swift:
我可以在 Swift 中这样称呼它:
let file = Test()
file.saveInBackgroundWithBlock({(success: Bool, error: NSError!) -> Void in
NSLog("1")
}, progressBlock: { (percentage: CInt) -> Void in
NSLog("2")
})
回答by Kreiri
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
).
(注意:从SWIFT代码调用的Objective-C时,Xcode的平移(在代码完成)int
进入CInt
,并NSInteger
进入Int
)。