2014-06-10 51 views
1

我在一些新的Swift代碼中使用FCModel來保存我的模型數據。在我的模式管理/遷移代碼中,當我對Int進行邏輯比較時,出現編譯時錯誤。我不知道這裏是否有語法或類型錯誤,或者是否發現了編譯器錯誤。爲什麼Swift編譯器會拋出:「找不到接受提供參數的'<'的重載」

這是一個有效的邏輯操作?我錯過了一個錯誤嗎?

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的幫助下,我認爲我已經收窄問題在哪裏,但我沒有ide爲什麼。再次,我認爲這可能是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 
    } 
} 

有什麼想法?

回答

4

聲明

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

沒有任何與你的錯誤,因爲你正在重新定義schemaVersion本地這裏:

... withSchemaBuilder: { database, schemaVersion in 

的方法被定義爲

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

所以我假設塊的類型爲schemaVersionCMutablePointer <CInt>

從邏輯上講,你不能直接比較它與Int1)。如果它是一個真正的CMutablePointer<Int>,你應該使用類似:

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

    ... 

    unsafeSchemaVersion.memory = 1 
} 

這相當於在的OBJ-C以下:

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

如果要引用已關閉外定義的變量,您應該重命名它或重命名閉包參數。

+0

Xcode檢查器顯示'schemaVersion'定義爲'CMutablePointer ',它沒有將'memory'定義爲屬性。 – blundin

+0

@blundin我在哪裏使用'schemaVersion.memory'? – Sulthan

+0

當我清理名稱衝突並專注於我的邏輯時,這對我有用。謝謝! – blundin

相關問題