[英]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)
}
}
}
它使用Alamofire和AlamofireObjectMapper 。 你能看到我錯過的東西嗎?
謝謝你的幫助
completionHandler
在fetchBeaconsFromRestApi
不應該拋出。 所以你應該用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.