Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot invoke 'saveInBackgroundWithBlock'

I've checked the syntax a gazillion times here, on GitHub, parse.com & elsewhere, without any luck. The problem is when I'm calling saveInBackgroundWithBlock for PFObject I get the the following error:

Cannot invoke 'saveInBackgroundWithBlock' with an argument list of type '((Bool, NSError) -> Void)'

I'm on Xcode 6.3 beta 2. All frameworks are loaded to the project (including Bolts & Parse, but not provided by parse.com ParseCrashReporting & ParseUI), <Parse/Parse.h> & even <Bolts/Bolts.h> are brought through the Bridge Header.

var score = PFObject(className: "score")
    score.setObject("Rob", forKey: "name")
    score.setObject(95, forKey: "scoreNumber")
    score.saveInBackgroundWithBlock {
        (success: Bool!, error: NSError) -> Void in
        if success == true {
            println("Score created with ID: \(score.objectId)")
        } else {
            println(error)
        }
    }

Any thoughts?

like image 250
IT Gypsy Avatar asked Mar 05 '15 22:03

IT Gypsy


2 Answers

The error parameter is supposed to be an implicitly unwrapped optional, but not the success one:

(success: Bool, error: NSError!) -> Void in
              ^               ^

However unless you need to specify the type for whatever reason, I suggest you to use the closure simply as:

(success, error) in

less prone to type declaration errors.

like image 175
Antonio Avatar answered Nov 17 '22 08:11

Antonio


In Swift 1.2 the declaration of .saveInBackgroundWithBlock should look like this:

Void saveInBackgroundWithBlock(block: PFBooleanResultBlock?(Bool, NSError?) -> Void)

So it should have been as follows:

score.saveInBackgroundWithBlock {
        (success: Bool, error: NSError?) -> Void in
like image 7
IT Gypsy Avatar answered Nov 17 '22 08:11

IT Gypsy