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

时间:2017-12-13 08:21:27

标签: sql arrays swift xcode sqlite.swift

如何将结果附加到此数组中以便以后使用?

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

这是我在上课时宣布的数组。

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)


            }

当我运行此代码时,我收到错误“无法调用非函数类型的值'[(orderlist:Int,questionid:String,question:String,description:String)]' “

row [4] row [0] row [6] row [7]返回一些我需要追加到数组中的值

1 个答案:

答案 0 :(得分:0)

尝试使用:

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

不要忘记:

let orderlist = row[4] as Int

您的代码不正确,因为:

1)要将新对象附加到数组,必须使用array-method

animalQuestion.append(<Array object>)

2)在你的情况下,数组对象是一个元组。首先,你需要创建一个元组,如:

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

之后,将appTuple命名为animalQuestion数组,如:

animalQuestion.append(orderTuple)