-1

My first time around here with a Swift related question with the SQLite.Swift library.

I have a for loop for a db.prepare statement, but I am stuck when trying to assign an array value to a UILabel.

    // Prepare query to retrieve the message
var _phraseMessage:String = ""    
let _stmt = _db.prepare("SELECT id, message FROM messages WHERE language = 'EN' AND category = 1 and username = 'user' LIMIT 1")

            for row in _stmt {

                println("id: \(row[0]), message: \(row[1])")

                self._phraseMessageLabel = row[1] --> i get an error here **"'Binding' is not convertible to UILabel"**


            }

How can assign the value in row[1] to my UILabel? Or even better to a String variable if possible.

Thanks in advance!

Disclaimer: This is my first attempt to Swift + Third party library

Undo
  • 25,519
  • 37
  • 106
  • 129
Alexof
  • 21
  • 6

2 Answers2

1

You're trying to set the UILabel property itself to your value - you want to set the text property on the label to your value:

self._phraseMessageLabel.text = row[1] as! String

If you want to assign the value to a variable:

var message = row[1] as! String

One thing to note is that with both approaches, your label text or variable will only end up being set to the value for the last row returned, the one that is processed last by the for loop.

stephencelis
  • 4,954
  • 2
  • 29
  • 22
Undo
  • 25,519
  • 37
  • 106
  • 129
0

Beyond Undo's fix above, I wanted to offer a couple other solutions.

  1. Try using Database.scalar or Statement.scalar if you're only using a single value, as in your example above.

    // make sure to remove "id" from your SELECT statement
    self._phraseMessageLabel.text = _stmt.scalar() as! String
    

    Note: Make sure to remove id from your SELECT statement; scalar returns the first column of the first row only.

  2. Use the Statement.row cursor.

    _stmt.step()
    self._phraseMessageLabel.text = _stmt.row[1]
    
stephencelis
  • 4,954
  • 2
  • 29
  • 22