从 parse.com 检索数据时出错 [英] Error while retrieving data from parse.com
问题描述
您好,我想从我的 parse.com 类中获取一些数据,该类名为Tags",该类中有两个 3 列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:unwrapping an Optional value 时意外发现 nil
的错误,请告诉我我的代码有什么问题.
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屋!