Fatal error: unexpectedly found nil while unwrapping an Optional value

0

I had 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 occur?

    
asked by anonymous 24.06.2015 / 21:08

1 answer

1

As you can see, the result of the SQLiteObjc.getText call may come empty, so it would be interesting for your function to return an optional string. I rewrote the code to handle the error. Hope it helps!

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

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