1
votes

I'm new to networking, Xcode, and Parse, and I was just following a YouTube tutorial but for some reason the code won't run. Here it is.

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

The error message says

Cannot invoke 'getObjectInBackgroundWithId' with an argument of type '(String, (PFObject!,NSError!) -> Void)

3
Well, what does the Parse documentation say this method looks like?nhgrif
He's using Swift 1.2. Parse docs are for 1.1nick9999
Well, then what does Swift try to autocomplete it into?nhgrif

3 Answers

0
votes

You must remove the PFObject! and NSError! ! mark the closure didn't return an optional type. try the follwing code

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

Not too sure if this can assist you.. it did helped me though in my coding..

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

....

0
votes

This is what worked for me:

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