带有进度的saveInBackgroundWithBlock在Xcode 6.3下的Swift中不起作用

时间:2015-04-15 17:36:21

标签: ios swift parse-platform

我从parse.com网站获得的以下代码不起作用:

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

错误消息是:

  

无法调用' saveInBackgroundWithBlock'参数列表   type'((Bool,NSError?) - > Void?,progressBlock:(Int) - > Void?)'

我尝试了不同的组合但没有效果。任何的想法?感谢

2 个答案:

答案 0 :(得分:2)

在IOS 8.3中,以下代码段有效:

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 ...
        if succeeded {
            println("Save successful")
        } else {
            println("Save unsuccessful: \(error?.userInfo)")
        }

        }, progressBlock: { (percentDone: Int32) -> Void in
        // Update your progress spinner here. percentDone will be between 0 and 100.
    })

请告诉我这是否适合您。

答案 1 :(得分:0)

我遇到了同样的问题,mrbcg的方式对我有用。

要显示进度:在Storyboard中添加并连接IBOutlet @IBOutlet weak var UploadProgressView: UIProgressView

然后编辑以下内容:

progressBlock: { (percentDone: Int32) -> Void in
            // Update your progress spinner here. percentDone will be between 0 and 100.
            let percentProgress = Float(percentDone) / 100.0
            let animated = percentProgress != 0
            self.UploadProgressView.setProgress(percentProgress, animated: animated)
    })

override func viewDidLoad() {
    super.viewDidLoad()
    UploadProgressView.setProgress(0, animated: true)
    // Do any additional setup after loading the view.
}