3

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!

7
  • 1
    See stackoverflow.com/a/25169265/1187415 for the sqlite3_column_text problems. Commented Nov 2, 2016 at 12:57
  • 1
    The last problem is a duplicate of stackoverflow.com/questions/39940937/error-in-for-loop-cgfloat (and why is there a += 1 in the loop at all?) Commented Nov 2, 2016 at 12:59
  • For the "'!=' produces 'Bool', not the expected contextual result type 'AnyObject?'" problem we would need to know more context: What does the function return? Commented Nov 2, 2016 at 13:03
  • @MartinR Sorry, forgot to add link to github for source code. Please see the edited post. I was wondering on the "+= 1" myself. Commented Nov 2, 2016 at 13:19
  • @MartinR For the != produces Bool, not the expected contextual result type AnyObject? the function is (after XCode Swift 3.0 migration tool) func getColumnValue(_ statement: OpaquePointer, index: Int32, type: String) -> AnyObject? { Commented Nov 2, 2016 at 14:27

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.