我尝试创建一些应用程序,但是我遇到了问题,我有这样的结构,问题和答案。
我的课:
class QuestionManager{
var question: String?
var answerA: String?
var answerB: String?
var answerC: String?
var answerD: String?
}
我的代码:
var questions = [QuestionManager]()
var arrayIndex = 0
let query = PFQuery(className: "test")
query.findObjectsInBackground{ (objects, error) in
if error == nil && objects != nil {
for object in objects as? [PFObject]{
self.questions.append(QuestionManager(
question: object!["question"] as! String,
answerA: object!["answer1"] as! String,
answerB: object!["answer2"] as! String,
answerC: object!["answer3"] as! String,
answerD: object!["answer4"] as! String
))
}
} else {
print("Error")
}
使用此代码会出现错误:
无法使用类型为((QuestionManager)''的参数列表调用“追加”
为什么我得到这个错误,也许你会看到我的错误
答案 0 :(得分:1)
替换此
var questions = [QuestionManager]()
var arrayIndex = 0
let query = PFQuery(className: "test")
query.findObjectsInBackground{ (objects, error) in
if error == nil && objects != nil {
for object in objects as? [PFObject] {
let objQuestionManager = QuestionManager()
objQuestionManager.question = object!["question"] as! String
objQuestionManager.answerA = object!["answer1"] as! String
objQuestionManager.answerB = object!["answer2"] as! String
objQuestionManager.answerC = object!["answer3"] as! String
objQuestionManager.answerD = object!["answer4"] as! String
self.questions.append(objQuestionManager)
}
} else {
print("Error")
}
答案 1 :(得分:1)
向带有4个输入值的类中添加初始化程序。
class QuestionManager{
var question: String?
var answerA: String?
var answerB: String?
var answerC: String?
var answerD: String?
init(quest: String?, ansA: String? , ansB: String? , ansC: String? , ansD: String){
question = ques
answerA = ansA
answerB = ansB
answerC = ansC
answerD = ansD
}
}
答案 2 :(得分:1)
由于没有在Model类中编写init()方法,并且正在尝试初始化模型类,因此出现此错误。您可以这样写。
class QuestionManager{
var question = ""
var answerA = ""
var answerB = ""
var answerC = ""
var answerD = ""
init(question: String, answerA: String, answerB: String, answerC: String, answerD: String){
self.question = question
self.answerA = answerA
self.answerB = answerB
self.answerC = answerC
self.answerD = answerD
}
}