-1

How can I append my results in this array so I can use it later ?

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

This is my array which I declared in my class above.

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)


            }

When i'm running this code , i'm getting the error "Cannot call value of non-function type '[(orderlist: Int, questionid: String, question: String, description: String)]'"

row[4] row [0]row [6]row [7] is returning some values which I need to append in my array

Ziyaad
  • 67
  • 8

1 Answers1

0

Try to use:

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

And do not forgot:

let orderlist = row[4] as Int

Your code incorrect because of:

1) For appending new objects to array you must to use array-method

animalQuestion.append(<Array object>)

2) In your case Array object is a tuple. So first you need to create a tuple, something like:

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

and after that appaned orderTuple to animalQuestion array like:

animalQuestion.append(orderTuple)
Serhii Didanov
  • 2,200
  • 1
  • 16
  • 31
  • Instance method 'append' expects a single parameter of type '(orderlist: Int, questionid: String, question: String, description: String)' – Ziyaad Dec 13 '17 at 09:02
  • Updated answer. – Serhii Didanov Dec 13 '17 at 09:07
  • Cannot convert value of type 'Binding?' to expected argument type 'Int' and Could not cast value of type 'Swift.Int64' (0x111b28110) to 'Swift.Int' (0x111b28430). Getting these as errors, still not working :/ – Ziyaad Dec 13 '17 at 09:10
  • let orderlist = row[4] as Int or Int(row[4]) – Serhii Didanov Dec 13 '17 at 09:13
  • Got it working! Thank for your help, here's the code : for row in stmt { let orderlist = row[4] as! Int64 let questionid = row[0] as! String let question = row[6] as! String let description = row[7] as! String animalQuestion.append((orderlist: Int(orderlist), questionid: questionid, question: question, description: description)) } – Ziyaad Dec 13 '17 at 09:18
  • The answer is correwct as far as it goes, but would be improved with an explanation of why it works and what a [tuple](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID329) is.. – JeremyP Dec 13 '17 at 09:33