Fatal error: unexpectedly found nil while unwrapping an Optional value

Asked

Viewed 403 times

0

I got the following error message:

Fatal error: unexpectedly found nil while unwrapping an Optional value

In the following code snippet:

    // Lê uma coluna do tipo String no (BANCO DE DADOS)
    func getString(stmt:COpaquePointer, index:CInt) -> String {

       let cString  = SQLiteObjc.getText(stmt, idx: index)

       let s = String(cString)

       return s
    }

Why does this happen?

  • 1

    The problem is identical to what occurred in your another question. Are you trying to start a String passing a null value, in case you need to check if cString has some value.

  • Thanks! I thought I could not do the same check, being that the value was null.

1 answer

1

From what you can see, the result of the Sqliteobjc.gettext call may be empty, so it would be interesting for your function to return an optional string. I rewrote the code to handle the bug. I hope it helps!

func getString(stmt:COpaquePointer, index:CInt) -> String? {

    if let cString = SQLiteObjc.getText(stmt, idx: index) {
        return String(cString)
    } else {
        return nil
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.