Tôi mới đến nhanh và
Tôi không hiểu tại sao tôi nhận được lỗi này ngay cả khi thực hiện xử lý do catch
.
Tôi đã được đọc câu hỏi tương tự và không cho đến nay trong số họ giải quyết lỗi này: Invalid conversion from throwing function type '(_) throws ->(). to non-throwing function type '([BeaconModel]) ->()'
tại dòng BeaconModel.fetchBeaconsFromRestApi(completionHandler: { .....
Swift 3 - Chuyển đổi không hợp lệ từ loại chức năng ném
Các đoạn mã với các lỗi:
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")
}
Các lấy phương pháp:
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)
}
}
}
Sử dụng Alamofire và AlamofireObjectMapper. Bạn có thể thấy những gì tôi đang thiếu?
Nhờ sự giúp đỡ
Cảm ơn, câu trả lời đã giải quyết được vấn đề! –