gpt4 book ai didi

ios - 使用 Swift 1.2 从 Parse 检索对象

转载 作者:行者123 更新时间:2023-11-28 09:09:39 24 4
gpt4 key购买 nike

我是网络、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")
}
}
}

错误信息是

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

最佳答案

您必须删除 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")
}
}
}

关于ios - 使用 Swift 1.2 从 Parse 检索对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29687009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com