I am using SQLite.swift (Branch Swift-1-2)
in my app in XCode 6.3 beta.
I am able to create the Database/Tables and insert entries into the tables.
So far so good.
Now when I have a simple scenario as follows:
class Foo {
var id: Int?
var name: String?
/* constructor to create an instance ... */
}
// This is how the table column is defined
var id = Expression<Int64>("id")
// Function to update data in the table
func updateFoo(foo: Foo) -> Int? {
// 'foos' is the table name
let candidateFoo = foos.filter(super.id == foo.id!) //<----ERROR!!!
let (rowsUpdated, statement) = candidateFoo.update(name <- foo.name!)
if let rowsUpdated = rowsUpdated {
println("Succesfully updated \(rowsUpdated) row(s)")
return rowsUpdated
} else if statement.failed {
println("Update failed. Reason: \(statement.reason)")
}
return nil
}
On the line commented \\ <----ERROR!!!
, I get the compile-time error:
Binary operator '==' cannot be applied to operands of type Expression< Int64 > and Int
If I use an Int
directly on that line, then that works fine. Eg.
let candidateFoo = foos.filter(super.id == 3) // No Error!
But, if I simply do this, it fails with the same error again:
var i = 3
let candidateFoo = foos.filter(super.id == i) // <----ERROR!!!
I understand what the error is, but I am not able to resolve it. I looked at the documentation but I am still stuck. So any help is appreciated.
Update:
Explicitly declaring the variable as Int64 solves the issue:
var i:Int64 = 3
let candidateFoo = foos.filter(super.id == i) // No Error!
Now I am wondering if I have to change my class definition, which will require changes at multiple places in the code. Also official swift documentation recommends using Int unless an explicit size is required.
You should use the word-sized Int type to store integer values, unless you require a type with a specific size or signedness.
Also, the SQLite.swift documentation states that:
While Int64 is the basic, raw type (to preserve 64-bit integers on 32-bit platforms), Int and Bool work transparently.
So should I be using the Int64
here explicitly in my class definition, since its mapped to a DB?