无法将“String”类型的值转换为预期参数类型“Int64”、表达式、[重复]

问题描述 投票:0回答:1
Cannot convert value of type 'String' to expected argument type 'Int64'

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

使用 SQLite pod 将项目更新/修改为最低 iOS 16.0+ 后,会出现这些错误

这个错误的核心在于不同的语言。

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)
    }
    
}
ios swift sqlite expression
1个回答
0
投票

解决方案#1:

Expression<String>("id")

对于这一行,只需添加 SQLite。结果我们应该:

SQLite.Expression<String>("id")

解决方案#2,全局修复:

typealias Expression = SQLite.Expression

import SQLite.Expression

把这个放在开头即可。我搜索了很多,但一开始没有找到任何类似的标题或解决方案来解决这个错误,因此也许它对某人有用。还有其他有趣的解决方案吗?

© www.soinside.com 2019 - 2024. All rights reserved.