我正在开发一个简单的iOS应用程序。这是一个简单的测验应用程序。我正在使用Parse存储我的问题,答案等。我已阅读所有文档,但无法找到检索对象的代码无效的原因。
var query = PFQuery(className: "Test_Questions")
query.getObjectInBackgroundWithId("cJzv2JdMej", block: {
(questionObject: PFObject?, error: NSError?) -> Void in
let thisQuestion = questionObject["question"] as! String
//Error here: AnyObject is not convertible to String
})
非常感谢您的帮助!
控制台输出:
可选(在else语句之前必须有什么语句?)
答案 0 :(得分:1)
你应该试试这个:
var query = PFQuery(className: "Test_Questions")
query.getObjectInBackgroundWithId("cJzv2JdMej", block: {
(questionObject: PFObject?, error: NSError?) -> Void in
if error == nil {
println(questionObject)
//if there's info in the console you know the object was retrieved
let thisQuestion = questionObject["question"] as? String
} else {
println("Error occurred retrieving object")
}
})
如果这不起作用,您也可以尝试let thisQuestion = questionObject.valueForKey("question")
。还要确保Parse实际上通过在检索后添加诸如println(questionObject)
之类的print语句来返回对象,以便您知道Parse正在返回一个对象。
答案 1 :(得分:0)
知道了!
var query = PFQuery(className: "Test_Questions")
query.getObjectInBackgroundWithId("cJzv2JdMej", block: {
(questionObject: PFObject?, error: NSError?) -> Void in
let thisQuestion: AnyObject! = questionObject!.valueForKey("question")
self.question.text = thisQuestion as? String
})