user594883
user594883

Reputation: 1351

SwiftData (SQLite wrapper) broken with Swift 3.0

I'm using SwiftData for SQLite access.

https://github.com/mozilla-mobile/firefox-ios/blob/master/Storage/ThirdParty/SwiftData.swift

SwiftData is a SQLite wrapper coded in Swift. After Swift 3.0 and XCode 8 the following lines are broken. I'm sort of noobie with Swift so I would appreciate your help with fixing what is broken:

let text = UnsafePointer<Int8>(sqlite3_column_text(statement, index))

results to: "'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type."

return Data(bytes: UnsafePointer<UInt8>(blob), count: Int(size))

results to: "Cannot invoke initializer for type 'UnsafePointer' with an argument list of type '(UnsafeRawPointer?)'"

return sqlite3_column_int(statement, index) != 0

results to: "'!=' produces 'Bool', not the expected contextual result type 'AnyObject?'"

let text = UnsafePointer<Int8>(sqlite3_column_text(statement, index))

Results to: "'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type."

for i: Int32 in 0 ..< columnCount += 1 {

Results to: "Left side of mutating operator isn't mutable: '..<' returns immutable value"

All Help Appreciated!

Upvotes: 3

Views: 1003

Answers (1)

Martin R
Martin R

Reputation: 539745

sqlite3_column_int(statement, index) returns an Int32 and

sqlite3_column_int(statement, index) != 0

is a boolean value, so that makes no sense as the return value if an (optional) AnyObject is expected. You can wrap the integer into a NSNumber instead:

func getColumnValue(_ statement: OpaquePointer, index: Int32, type: String) -> AnyObject? {
    let val = sqlite3_column_int(statement, index)
    return NSNumber(value: val)
}

Another option would be

func getColumnValue(_ statement: OpaquePointer, index: Int32, type: String) -> AnyObject? {
    return sqlite3_column_int(statement, index) as AnyObject
}

because in Swift 3, anything can be converted to AnyObject. The difference is that in the second solution, the object can only be converted back to the original number type Int32, but not to Int or any other integer type.

For the other problems, see

Upvotes: 1

Related Questions