1

I am reading from a dbtable and get an error at a specific position of the table. My sql is ok, because I could already read from the same table, but at a specific row I get an error and I would like to know howto handle this error. I am not looking for a solution to solve my db-issue, I am just looking for handling the error, so it doesn't crash.

I have the following code :

let unsafepointer=UnsafePointer<CChar>(sqlite3_column_text(statement, 2));
if unsafepointer != nil {
    sText=String.fromCString(unsafepointer)! // <<<<<< ERROR
} else {
   sText="unsafe text pointer is nil !";
}

I get an error:

"fatal error: unexpectedly found nil while unwrapping an Optional value"

at line marked with <<<<<< ERROR.

The unsafe pointer's value is not nil:

pointerValue : 2068355072

How can I handle this error, so my app is not crashing ?

Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
mcfly soft
  • 11,289
  • 26
  • 98
  • 202

3 Answers3

3

Another possible solution is this:

let unsafepointer=UnsafePointer<CChar>(sqlite3_column_text(statement, 2));
var sText = "unsafe text pointer is nil !";
if unsafepointer != nil{
    if let text = String.fromCString(unsafepointer) as String?{
        sText = text;
    }    
}
ciccioska
  • 1,291
  • 16
  • 22
1
if sqlite3_column_text(statement, 2) != nil {                     
print("do something")
} else {
YourString = ""
}
0
let statementValue = sqlite3_column_text(statement, 2)
var sText = withUnsafeMutablePointer(&statementValue) {UnsafeMutablePointer<Void>($0)}
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133