【问题标题】:Why does the Swift compiler throw: "Could not find an overload for '<' that accepts the supplied arguments"为什么 Swift 编译器会抛出:“找不到接受提供的参数的 '<' 的重载”
【发布时间】:2014-07-30 17:20:11
【问题描述】:

我在一些新的 Swift 代码中使用 FCModel 来保存我的模型数据。在我的模式管理/迁移代码中,当我对 Int 执行逻辑比较时出现编译时错误。我无法判断这里是否存在语法或类型错误,或者我是否发现了编译器错误。

这是一个有效的逻辑运算吗?有没有我遗漏的错误?

Error: AppDelegate.swift:53:30: Could not find an overload for '<' that accepts the supplied arguments

相关代码:

var schemaVersion: Int = (settings["schemaVersion"] as String).toInt()!

FCModel.openDatabaseAtPath(databasePath, withSchemaBuilder: { database, schemaVersion in
    database.crashOnErrors = true
    database.traceExecution = true
    database.beginTransaction()

// 'failedAt' closure removed for brevity

if schemaVersion < 1 { // ERROR HAPPENS HERE
    if !database.executeUpdate("SQL STATEMENT HERE", withArgumentsInArray: nil)
    { failedAt(1) }

    schemaVersion = 1
}

更新 openDatabaseAtPath() 定义为:

func openDatabaseAtPath(path: String!, withSchemaBuilder schemaBuilder: ((FMDatabase!, CMutablePointer<CInt>) -> Void)!)

更新 #2 在下面 Sulthan 的帮助下,我想我已经缩小了问题所在,但我不知道为什么。同样,我认为这可能是 Swift 中的一个错误。

更新代码:

var schemaVersion: Int = 0
let database: FMDatabase = FMDatabase(path: databasePath)

FCModel.openDatabaseAtPath(databasePath, withSchemaBuilder: { (database, schemaVersion: CMutablePointer<CInt>) in
    var x:Int = Int(schemaVersion.withUnsafePointer( { (unsafeSchemaVersion: UnsafePointer<CInt>) -> CInt in
        return unsafeSchemaVersion.memory
    }))
    ...
})

当我在 Xcode 中调试和使用 REPL 时,会发生一些有趣的事情。当执行在 withSchemaBuilder 块中时,database 不为零,并且匹配上面创建的对象。 schemaVersion 不是可识别的标识符:

error: use of unresolved identifier 'schemaVersion'
schemaVersion

和:

$R0: FMDatabase! = {
  Some = {
    NSObject = {
      isa = FMDatabase
    }
    _db =
    _databasePath = @"/Users/brian/Library/Developer/CoreSimulator/Devices/25115FDE-0DA8-4F2B-B4E5-5A0600720772/data/Containers/Data/Application/579A64D7-6C6B-4690-B503-2CA5B4229D55/Documents/userData.sqlite"
    _logsErrors = YES
    _crashOnErrors = NO
    _traceExecution = NO
    _checkedOut = NO
    _shouldCacheStatements = NO
    _isExecutingStatement = NO
    _inTransaction = NO
    _maxBusyRetryTimeInterval = 2
    _startBusyRetryTime = 0
    _cachedStatements = nil
    _openResultSets = 0 objects
    _openFunctions = nil
    _dateFormat = nil
  }
}

有什么想法吗?

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    声明

    var schemaVersion: Int = (settings["schemaVersion"] as String).toInt()!
    

    与您的错误没有任何关系,因为您在此处本地重新定义 schemaVersion

    ... withSchemaBuilder: { database, schemaVersion in 
    

    方法定义为

    + (void)openDatabaseAtPath:(NSString *)path
             withSchemaBuilder:(void (^)(FMDatabase *db, int *schemaVersion))schemaBuilder;
    

    所以我假设块中schemaVersion 的类型类似于CMutablePointer &lt;CInt&gt;

    从逻辑上讲,您不能直接将其与Int (1) 进行比较。如果它真的是CMutablePointer&lt;Int&gt;,你应该使用类似:

    schemaVersion.withUnsafePointer { unsafeSchemaVersion in
        if unsafeSchemaVersion.memory < 1
    
        ...
    
        unsafeSchemaVersion.memory = 1
    }
    

    这相当于 Obj-C 中的以下内容:

     if (*schemaVersion < 1) {
         ...
         *schemaVersion = 1;
     }
    

    如果你想引用你在闭包外定义的变量,你应该重命名它或者重命名闭包参数。

    【讨论】:

    • Xcode 检查器显示schemaVersion 定义为CMutablePointer&lt;CInt&gt;,它没有将memory 定义为属性。
    • @blundin 我在哪里使用schemaVersion.memory
    • 当我清理名称冲突并专注于我的逻辑时,这对我有用。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-08-02
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多