使用Swift 1.2从Parse中检索对象

时间:2015-04-16 22:20:29

标签: ios swift parse-platform

我是网络,Xcode和Parse的新手,我刚刚关注了YouTube教程,但由于某些原因,代码无法运行。在这里。

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

错误消息显示

  

无法使用类型为'(String,(PFObject!,NSError!) - > Void)的参数调用'getObjectInBackgroundWithId'

3 个答案:

答案 0 :(得分:0)

你必须删除PFObject!和NSError! !标记闭包没有返回可选类型。尝试下面的代码

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

答案 1 :(得分:0)

不太确定这是否可以帮助你..虽然我的编码确实帮助了我..

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

...

答案 2 :(得分:0)

这对我有用:

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