简体   繁体   English

Swift解析框架和闭包

[英]Swift parse framework and closures

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 .. 我试图在swift中使用getFirstObjectInBackgroundWithBlock()方法,但是我不知道如何(不)使用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)'" 我只是得到一个“无法使用类型为(((PFObject ?, NSError?)-> Void)'的参数列表来调用'getFirstObjectInBackgroundWithBlock'””

Can you pleaase help me? 你能帮我个忙吗? Thank you 谢谢

This error that you are getting is, as you've guessed already that you need to hvae gameScore object as an optional. 正如您已经猜到的那样,您遇到的这个错误是需要将gameScore对象作为可选对象。

"Cannot invoke 'getFirstObjectInBackgroundWithBlock' with an argument list of type '((PFObject?, NSError?) -> Void)'" “无法使用类型为'(((PFObject ?, NSError?)-> Void)'的参数列表来调用'getFirstObjectInBackgroundWithBlock'”

This is not because of swift or its limitations. 这不是因为迅速或其局限性。 It's because Parse SDK defines that function like that. 这是因为Parse SDK定义了该函数。 And unless Parse changes its API, you will have to use an optional. 除非Parse更改其API,否则您将必须使用可选的。

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. 您将得到一个PFObject或得到一个Error,而不是两者都得到。 So one of them will be nil, hence the use of Optional. 因此其中之一将为nil,因此使用Optional。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM