Nurdin
Nurdin

Reputation: 23911

Swift/Parse - Cannot invoke 'getObjectInBackgroundWithId' with an argument list of type '(PFUser?, (PFObject?, NSError?) -> Void)'

I got this error message when trying to search object UserDetail.

Error message

/Users/MNurdin/Documents/iOS/xxxxx/ViewController.swift:125:15: Cannot invoke 'getObjectInBackgroundWithId' with an argument list of type '(PFUser?, (PFObject?, NSError?) -> Void)'

My code

var user = PFUser.currentUser()

        var query = PFQuery(className:"UserDetail")
        query.getObjectInBackgroundWithId(user) { //error message here
            (gameScore: PFObject?, error: NSError?) -> Void in
            if error != nil {
                println(error)
            } else if let gameScore = gameScore {
                gameScore["cheatMode"] = true
                gameScore["score"] = 1338
                gameScore.saveInBackground()
            }
        }

Upvotes: 1

Views: 420

Answers (3)

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71862

Use getObjectInBackgroundWithId this way:

query.getObjectInBackgroundWithId("yourUser", block: {
        (gameScore: PFObject?, error: NSError?) -> Void in

        //your code

    })

As you can see syntax for getObjectInBackgroundWithId is:

enter image description here

Upvotes: 1

kernelpanic
kernelpanic

Reputation: 2956

I'm not expert, but looking at documentation I see it accepts String as parameter instead of object.

Try using:

query.getObjectInBackgroundWithId(user?.objectId)

Upvotes: 1

Stuart
Stuart

Reputation: 37053

You are using the wrong parameter types in the block passed to PFQuery's getObjectInBackground(_:block:) method. According to the docs, the block argument takes an NSArray (this can be bridged to a Swift array of AnyObject), and an error:

query.getObjectInBackgroundWithId(user?.objectId) { (object: [AnyObject]?, error: NSError?) -> Void in
    // ...
}

In addition, the objectId parameter should be a string, but you appear to be passing in a PFUser instance.

Upvotes: 2

Related Questions