0

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

4

1 回答 1

2

你得到的这个错误是,你已经猜到你需要将 gameScore 对象作为可选对象。

“无法使用“((PFObject?,NSError?)-> Void)'类型的参数列表调用'getFirstObjectInBackgroundWithBlock'”

这不是因为 swift 或其局限性。这是因为 Parse SDK 是这样定义的。除非 Parse 更改其 API,否则您将不得不使用可选的。

在这件事上只有我的两分钱,一个可选的在这里是有序的。要么你会得到一个 PFObject 要么你会得到一个错误,而不是两者兼而有之。所以其中之一将是 nil,因此使用 Optional。

于 2015-06-05T17:01:26.913 回答