Alfi Alfi - 5 months ago 45
Swift Question

how sum String array numbers from a sqlite column in swift

I have a sqlite table on my iOS app and I want to sum all String numbers of a column of it. the column name is : "count"

I Wrote this code to get the numbers :

let kiwi = DB.getInstance()
let records = kiwi.executeQuery("SELECT * FROM zekrlist")
for record in records {
let structContact = StructContact()
structContact.id = record.column["id"]?.asInt()
structContact.count = record.column["count"]?.asString()
print(structContact.count)


the print result is like this:

4

2

i want sum of this numbers ! (6) !

any help will be appreciated.

Answer
let kiwi = DB.getInstance()
let count:Int = 0
let records = kiwi.executeQuery("SELECT * FROM zekrlist")
for record in records {
    let structContact = StructContact()
    structContact.id = record.column["id"]?.asInt()
    structContact.count = record.column["count"]?.asString()
    count = count + structContact.count.asInt()  // Int(structContact.count)
}
print(count)