user594883 user594883 - 2 months ago 36
Swift Question

Swift 2.x -> Swift 3.0 conversion issue with String.fromCString

I have the following Swift 2.x syntax:

if let columnType = String.fromCString(sqlite3_column_decltype(pStmt, i))?.uppercased() {

I'm trying to convert my code to swift 3.0. However I can't get this statement to work. Currently I have the following but its giving me: "Cannot use optional chaining on non-optional value of type 'String'"

if let columnType = String(cString: sqlite3_column_decltype(pStmt, i))?.uppercased() {


String(cString:) in Swift 3 does not return an optional:

let columnType = String(cString: sqlite3_column_decltype(pStmt, i)).uppercased()

Invalid UTF-8 bytes are replaced by the Unicode replacement character U+FFFD.

Or use String(validatingUTF8:) which returns an optional (nil if the input is not a valid UTF-8 sequence):

if let columnType = String(validatingUTF8: sqlite3_column_decltype(pStmt, i))?.uppercased() {
    // ...