[英]Swift: 2 consecutive closures/blocks
根據這里的文檔: 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:
另一種嘗試,不同的錯誤:
編輯3:原始代碼,但根據評論建議使用CInt
我使用方法簽名在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")
})
你在方法參數周圍缺少()。 應該:
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
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.