如何在for循环中附加到数组?

How to append to array in for loop?

如何将我的结果追加到这个数组中以便以后使用?

 var animalQuestion = [(orderlist: Int, questionid:String, question: String, description: String)]()

这是我在上面 class 中声明的数组。

let stmt = try db.prepare("SELECT * FROM allquestion_other where allquestion_other.name= 'animalChoice' and allquestion_other.id NOT IN (select answerresult.questionId FROM answerresult where answerresult.friendUserId='\(friendUserId)')")
        for row in stmt {

            let orderlist = row[4]
            let questionid = row[0]
            let question = row[6]
            let description = row[7]

        animalQuestion.append(orderlist: orderlist, questionid: questionid, question: question, description: description)


            }

当我运行这段代码时,我收到错误"Cannot call value of non-function type '[(orderlist: Int, questionid: String, question: String, description: String)]'"

行[4]行[0]行[6]行[7]正在返回一些我需要附加到数组中的值

尝试使用:

animalQuestion.append((orderlist: orderlist, questionid: questionid, question: question, description: description))

别忘了:

let orderlist = row[4] as Int

您的代码不正确,因为:

1) 要将新对象附加到数组,您必须使用数组方法

animalQuestion.append(<Array object>)

2) 在您的例子中,数组对象 是一个元组。所以首先你需要创建一个元组,比如:

let orderTuple = (orderlist, questionid, question, description)

然后将 orderTuple 应用到 animalQuestion 数组,例如:

animalQuestion.append(orderTuple)