Error while retrieving data from parse.com

我们两清 提交于 2019-12-02 14:11:19

The problem is that:

let username = gameScore["username"] as! String
let tagtext = gameScore["tagtext"] as! String

gameScore["username"] and gameScore["tagtext"] can return nil values, and when you say as! String you say that it will be a String, and it is nil.

Try something like:

let username = gameScore["username"] as? String
let tagtext = gameScore["tagtext"] as? String

your error is happening because of that, but your final code should look like this:

@IBAction func readAction(sender: UIButton) {

  var query = PFQuery(className:"Tags")
  query.getObjectInBackgroundWithId("f3AXazT9JO") {
    (gameScore: PFObject?, error: NSError?) -> Void in

    let username = gameScore["username"] as? String
    let tagtext = gameScore["tagtext"] as? String

    println(username)
    println(tagtext) 
    if error == nil && gameScore != nil {
       println(gameScore)
    } else {
       println(error)
    }
  }
}

Because the getObjectInBackgroundWithId is async.

You are trying to read from your response object gameScore but it is still nil because getObjectInBackgroundWithId is an asynchronous method meaning that it will return a result once it is finished. Put the two lines inside the handler and start from there.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!