簡體   English   中英

Swift 3 - 投擲函數類型的轉換無效

[英]Swift 3 - Invalid conversion from throwing function type

我很快就快
我不明白為什么即使做了do catch治療我也會收到這個錯誤。

我正在閱讀類似的問題,到目前為止,他們都沒有解決這個錯誤:
Invalid conversion from throwing function type '(_) throws -> (). to non-throwing function type '([BeaconModel]) -> ()' Invalid conversion from throwing function type '(_) throws -> (). to non-throwing function type '([BeaconModel]) -> ()'BeaconModel.fetchBeaconsFromRestApi(completionHandler: { .....BeaconModel.fetchBeaconsFromRestApi(completionHandler: { .....

帶有錯誤的代碼段:

do{

    let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    let db = try Connection("\(path)/db.sqlite3")

    let beaconsTbl = Table("beacons")
    let id = Expression<Int64>("id")
    let uuid = Expression<String>("uuid")
    let major = Expression<String>("major")
    let minor = Expression<String>("minor")

    try db.run(beaconsTbl.create { t in
        t.column(id, primaryKey: true)
        t.column(uuid)
        t.column(major)
        t.column(minor)
    })

    BeaconModel.fetchBeaconsFromRestApi(completionHandler: {
        beacons in
        for item in beacons{

            let insert = beaconsTbl.insert(id <- item.id!, uuid <- item.uuid!, major <- item.major!, minor <- item.minor!)
            try db.run(insert)
        }
    })

} catch {
    print("Error creating the database")
}

獲取方法:

static func fetchBeaconsFromRestApi( completionHandler: @escaping (_ beacons: [BeaconModel]) -> ()){

    Alamofire.request(Constants.Beacons.URLS.ListAllbeacons).responseArray(keyPath: "data") { (response: DataResponse<[BeaconModel]>) in

        let beaconsArray = response.result.value
        if let beaconsArray = beaconsArray {
            completionHandler(beaconsArray)
        }
    }
}

它使用AlamofireAlamofireObjectMapper 你能看到我錯過的東西嗎?

謝謝你的幫助

completionHandlerfetchBeaconsFromRestApi不應該拋出。 所以你應該用do - catch包裝所有的throw調用:

BeaconModel.fetchBeaconsFromRestApi(completionHandler: {
    beacons in
    for item in beacons{

        let insert = beaconsTbl.insert(id <- item.id!, uuid <- item.uuid!, major <- item.major!, minor <- item.minor!
        do {
            try db.run(insert)
        } catch {
            print("Error creating the database")
        }
    }
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM