使用Swift从Parse中检索对象1.2

2020年11月24日 70点热度 0条评论 来源: Vincent Landolfi

我刚接触网络,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") 
     } 
    } 
} 
    原文作者:Vincent Landolfi
    原文地址: https://stackoverflow.com/q/29687009
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。