mcfly soft mcfly soft - 5 months ago 15
Swift Question

unexpectedly found nil while unwrapping an Optional value while reading from DS with fromCString

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

pointerValue : 2068355072

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


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;