使用Swift从Parse中检索对象1.2
我刚接触网络,Xcode和Parse,并且我只是遵循YouTube教程,但由于某些原因代码无法运行。这里是。使用Swift从Parse中检索对象1.2
func CallData(){ var query : PFQuery = PFQuery(className: "QuestionsAndAnswers")
query.getObjectInBackgroundWithId("DZfARcuav8"){
(ObjectHolder : PFObject!, error : NSError!) -> Void in
if (error == nil){
self.Question = ObjectHolder["Question"] as! String
self.Answers = ObjectHolder["Answers"] as! Array
self.Answer = ObjectHoler["Answer"] as! String
if (self.Answers.count > 0){
self.QuestionLabel.text = self.Question
self.Button1.setTitle(self.Answers[0], forState: UIControlState.Normal)
self.Button1.setTitle(self.Answers[1], forState: UIControlState.Normal)
self.Button1.setTitle(self.Answers[2], forState: UIControlState.Normal)
self.Button1.setTitle(self.Answers[3], forState: UIControlState.Normal)
}
}
else{
NSlog("There is something wrong")
}
}
}
错误消息说
不能援引 'getObjectInBackgroundWithId' 类型的参数“(字符串,(PFObject,NSError)! - >无效)
回答:
你必须删除PFObject!和NSError! !标记闭包没有返回一个可选类型。尝试follwing代码
func CallData(){ var query : PFQuery = PFQuery(className: "QuestionsAndAnswers")
query.getObjectInBackgroundWithId("DZfARcuav8"){
(ObjectHolder, error) -> Void in
if (error == nil){
let object = ObjectHolder as [NSObject: AnyObject]
self.Question = object["Question"] as String
self.Answers = object["Answers"] as Array
self.Answer = object["Answer"] as String
if (self.Answers.count > 0){
self.QuestionLabel.text = self.Question
self.Button1.setTitle(self.Answers[0], forState: UIControlState.Normal)
self.Button1.setTitle(self.Answers[1], forState: UIControlState.Normal)
self.Button1.setTitle(self.Answers[2], forState: UIControlState.Normal)
self.Button1.setTitle(self.Answers[3], forState: UIControlState.Normal)
}
}
else{
NSlog("There is something wrong")
}
}
}
回答:
不太清楚,如果这能帮助你..它没有在我的编码虽然帮我..
func CallData(){ var query : PFQuery = PFQuery(className: "QuestionsAndAnswers")
query.getObjectInBackgroundWithId("DZfARcuav8"){
(ObjectHolder, error) -> Void in
if (error == nil){
ObjectHolder!.setValue(self.Question, forKey: "Question")
ObjectHolder!.setValue(self.Answers, forKey: "Answers")
ObjectHolder!.setValue(self.Answer, forKey: "Answer")
....
回答:
这是什么工作对我来说:
var query = PFQuery(className: "score") query.getObjectInBackgroundWithId("j5xBfJ9YXu", block: {
(obj, error)in
if let score = obj! as? PFObject {
println(score.objectForKey("name"))
} else {
println(error)
}
})
以上是 使用Swift从Parse中检索对象1.2 的全部内容, 来源链接: utcz.com/qa/266459.html