从parse.com检索数据时出错 [英] Error while retrieving data from parse.com

查看:73
本文介绍了从parse.com检索数据时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想从parse.com类中的标签"中获取一些数据,该类中有两个3个cols"objectID","username"和"tagtext".我想读取通过ID和后缀查找的记录,我想将用户名"和标记文字"保存到两个字符串中.我已经做到了,就像在parse.com文档中一样:

Hello I want to get some data from my parse.com class called "Tags" in this class there are two 3 cols "objectID", "username" and "tagtext". I want to read a record finding by ID and afterwords I want to save "useername" and "tagtext" into two strings. I have done it like it is in the parse.com documentation:

@IBAction func readAction(sender: UIButton) {

    var query = PFQuery(className:"Tags")
    query.getObjectInBackgroundWithId("IsRTwW1dHY") {
        (gameScore: PFObject?, error: NSError?) -> Void in
        if error == nil && gameScore != nil {
            println(gameScore)
        } else {
            println(error)
        }
    }

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

    println(username)
    println(tagtext)   

}

我收到一个名为fatal error: unexpectedly found nil while unwrapping an Optional value的错误,请告诉我我的代码出了什么问题.

I get an error called fatal error: unexpectedly found nil while unwrapping an Optional value , please tell me what is wrong in my code.

我的课:

推荐答案

问题是:

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

gameScore["username"]gameScore["tagtext"]可以返回nil值,当您说as! String时,您说它将是一个String,并且它是nil.

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.

尝试类似的东西:

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)
    }
  }
}

因为getObjectInBackgroundWithId是异步的.

这篇关于从parse.com检索数据时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆