Swift parse framework and closures

152 views Asked by At

This is srsly driving me crazy.

I am trying to use getFirstObjectInBackgroundWithBlock() method in swift but I can't figure out how to (not) use the optionals ..

I just want to get the user's score from the parse server And I do it like this:

func updateScoreForCurrentUser(score: Int){
    let user = PFUser.currentUser()

    // get gameScore for user
    var query = PFQuery(className: "GameScore")
    query.whereKey("User", equalTo: user!)
    query.getFirstObjectInBackgroundWithBlock { (gameScore: PFObject, error: NSError?) -> Void in
        gameScore["score"] = score
}

I just get a "Cannot invoke 'getFirstObjectInBackgroundWithBlock' with an argument list of type '((PFObject?, NSError?) -> Void)'"

Can you pleaase help me? Thank you

1

There are 1 answers

2
Shamas S On BEST ANSWER

This error that you are getting is, as you've guessed already that you need to hvae gameScore object as an optional.

"Cannot invoke 'getFirstObjectInBackgroundWithBlock' with an argument list of type '((PFObject?, NSError?) -> Void)'"

This is not because of swift or its limitations. It's because Parse SDK defines that function like that. And unless Parse changes its API, you will have to use an optional.

And just my two cents on the matter, an Optional is in order here. Either you will get a PFObject or you will get an Error, not both. So one of them will be nil, hence the use of Optional.