iosswiftsqliteexpression

Cannot convert value of type 'String' to expected argument type 'Int64', expression,


Cannot convert value of type 'String' to expected argument type 'Int64'

or

Cannot convert value of type 'Expression<String>' to expected argument type 'Expression<Int64?>'

These errors appear after the project has been updated/modified to minimum iOS 16.0+ using SQLite pods.

The core of this error is in different languages.

import Foundation
import SQLite

class Sql {
    
    var i = [Int64]()
    var c = [UIColor]()
    var n = [String]()
    var d = [String]()
    
    func read() -> ([Int64], [UIColor], [String], [String]){
        
        // Wrap everything in a do...catch to handle errors
        do {
            let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
            let dbFile = "\(path.first ?? "")/db.sqlite3"
            let db = try Connection(dbFile)

            let history =   Table("history")
            let id =        Expression<Int64>(value:    "id")
            let dbname =    Expression<String?>(value:  "name")
            let dbr =       Expression<Int>(value:      "r")
            let dbg =       Expression<Int>(value:      "g")
            let dbb =       Expression<Int>(value:      "b")
            let dbdate =    Expression<Date>(value:     "date")

            let df = DateFormatter()
            df.dateFormat = "yyyy-MM-dd HH:mm:ss"
            
            let exist = try db.scalar(history.exists)
            if !exist {
                do {
                    print("not exist - create DB")
                    try db.run(history.create { t in
                        t.column(id, primaryKey: true)
                        t.column(dbname)
                        t.column(dbr)
                        t.column(dbg)
                        t.column(dbb)
                        t.column(dbdate)
                    })
                } catch {
                    print("catch error - creating database for save")
                    print(error)
                }
                print("create successful")
            } else {
                print("exist db already")
            }
            i.removeAll()
            c.removeAll()
            n.removeAll()
            d.removeAll()
            
            // check print
            for color in try db.prepare(history.order(dbdate.desc)) {

                i.append(color[id])
                c.append(UIColor(
                    red:    CGFloat((Double((color[dbr])) ?? 0)/255.0),
                    green:  CGFloat((Double((color[dbg])) ?? 0)/255.0),
                    blue:   CGFloat((Double((color[dbb])) ?? 0)/255.0), alpha: 1.0))
                n.append(color[dbname])
                let readDate = df.string(from: color[dbdate])
                d.append(readDate)
            }
            
        } catch {
            print("catch. Stopped before read :(")
            print (error)
        }
        return (i, c, n, d)
    }
    
}

Solution

  • Solution #1:

    Expression<String>("id")
    

    For this line just simply add SQLite. As a result we should have:

    SQLite.Expression<String>("id")
    

    Solution #2, global fix:

    typealias Expression = SQLite.Expression
    

    or

    import SQLite.Expression
    

    Just put this at the beginning. I searched a lot but didn't find any similar titles or solutions for this error at first, therefore maybe it would be useful for someone. Any other interesting solutions?