2015-04-16 58 views
1

我剛接觸網絡,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)! - >無效)

+0

那麼,Parse文檔說這個方法看起來像什麼? – nhgrif

+0

他使用Swift 1.2。解析文檔是爲1.1 – nick9999

+0

那麼,Swift試圖自動完成它到什麼? – nhgrif

回答

0

你必須刪除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") 
     } 
    } 
} 
+0

這樣做後,來自getObject行的錯誤消失了,但是在我設置變量的位置出現了另外6個錯誤。感謝您的幫助。 –

+0

你必須刪除!符號。只是看到更新的答案。並請將答案標記爲已解決。以幫助其他人找到真正的答案 –

+0

是的,我的代碼與您的代碼完全相同,但在第一條if語句之後的三行中仍然存在三個錯誤。這是說它不能分配值,或者該對象不能轉換爲數組 –

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

....

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