Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftData (SQLite wrapper) broken with Swift 3.0

Tags:

sqlite

ios

swift

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!

like image 664
user594883 Avatar asked Jun 22 '26 15:06

user594883


1 Answers

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

  • Issue with UnsafePointer<Uint8> in SQLite project in Swift
  • Error in for loop CGFloat
like image 159
Martin R Avatar answered Jun 24 '26 05:06

Martin R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!