I am getting wrong value from sqlite table in swift -
i getting wrong value sqlite table. code
code :
var arrtribute_mast_list : nsmutablearray = [] var querysql = string(format:"select * %@ order %@", table_attribute_master, attribute_master_attr_id) var statement:copaquepointer = nil //querysql = querysql.string(nsutf8stringencoding, allowlossyconversion: false) if (sqlite3_prepare_v2(database, querysql, -1, &statement, nil) == sqlite_ok) { while(sqlite3_step(statement) == sqlite_row) { let name = sqlite3_column_text(statement, 2) let attributename = string.fromcstring(unsafepointer<cchar>(name)) println("attribute value = \(attributename)") } sqlite3_finalize(statement); }
i getting output:
attribute value = optional("shamitabh")
need output :
attribute value = "shamitabh"
you getting optional("shamitabh")
because string.fromcstring
not guaranteed succeed. may fail, , attributename
nil. can resolve either making implicitly unwrapped optional (which crash if string.fromcstring
fails):
let attributename = string.fromcstring(unsafepointer<cchar>(name))!
.. or can right , check if value nil before using it. example, this:
if let attributename = string.fromcstring(unsafepointer<cchar>(name)) { println("attribute value = \(attributename)") } else { println("couldn't attribute name") }
Comments
Post a Comment