iOS如何从数组Swift

时间:2018-08-02 13:15:11

标签: ios arrays swift

我有一个应用程序,其中有一个问题,我将其保存在数组中,如果我得到100个值,我将得到错误:

保存数据的类:

class QuestionManager {
var question: String?
var answerA: String?
var answerB: String?
var answerC: String?
var answerD: String?

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
}

从服务器加载数据的功能:

    var questions = [QuestionManager]()
    let query = PFQuery(className: dbQuery.query)
    query.findObjectsInBackground{ (objects, error) in
        if error == nil && objects != nil {
            for object in objects! {

                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["answer5"] as! String

                ))
            }
            self.questionText = questions[self.arrayIndex].question!
            self.answer1 = questions[self.arrayIndex].answerA!
            self.answer2 = questions[self.arrayIndex].answerB!
            self.answer3 = questions[self.arrayIndex].answerC!
            self.answer4 = questions[self.arrayIndex].answerD!
            self.loadDataToBox()
        } else {
            print("Error")
        }

    }
  

致命错误:索引超出范围

我有一个问题清单,其中有300多个问题,我该如何解决?

1 个答案:

答案 0 :(得分:3)

PFQuery上的默认limit是100个对象。如果要检索更多信息(最多1000个),则需要在执行抓取操作之前设置limit属性。

如果要检索1000个以上的值,则需要发出其他查询。