In a Swift + SQLite project I'm trying to populate an array of 4 buttons in my UI with a SQL query. The best I could do so far is as the code below shows, but I'm sure it can be done in a cleaner way. I tried to do both the SQL query result reading and button change in the same loop but got all sorts of type mismatch and reference errors.
Any suggestion will be much appreciated.
var answerText: Array<String?> = ["","","",""]
class ViewController: UIViewController {
@IBOutlet var answerButton: Array<UIButton> = []
func displayQuestion(questionNumber: Int){
let querySQL = "SELECT answer FROM answers WHERE answers.question = '\(questionNumber)'"
let results:FMResultSet? = myDatabase.executeQuery(querySQL,
withArgumentsInArray: nil)
var j=0
while results?.next() == true {
answerText[j] = results?.stringForColumn("answer")
j=j+1
}
j=0
for item in answerButton{
var button:UIButton = item as UIButton
button.setTitle(answerText[j], forState: UIControlState.Normal)
j=j+1
}
}
}