lundi 14 septembre 2015

Append FMDB SQLite results to Swift array

I'm trying to append results from a FMDB SQLite query to a Swift array. The error I'm getting in XCode is 'value of option type 'String?' not unwrapped.'

Swapping out the line below in the while loop, the FMDB results can be printed to console OK, so no problems there.

println(results_lab_test?.stringForColumn("lab_test"))

New to XCode, so be kind...

var arrayData:[String]

let filemgr = NSFileManager.defaultManager()

let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)

let docsDir = dirPaths[0] as! String

databasePath = docsDir.stringByAppendingPathComponent("vmd_db.db")

let myDatabase = FMDatabase(path: databasePath as String)

if myDatabase.open(){

let query_lab_test = "SELECT lab_test FROM lab_test"

let results_lab_test:FMResultSet? = myDatabase.executeQuery(query_lab_test, withArgumentsInArray: nil)

      while results_lab_test?.next() == true {
                       arrayData.append(results_lab_test?.stringForColumn("lab_test"))
          }
       }

Aucun commentaire:

Enregistrer un commentaire