这个方法在我使用Swift 1.2时正常工作。但现在,我不得不更新到Xcode,我不得不将我的语言切换到Swift 2.这是swift 1.2的方法,我用得很好;
static func findById(idToFind : Int64) -> T? {
let query = table.filter(id == idToFind)
var results: Payment?
if let item = query.first {
results : T = Payment(id: item[id], imageName: item[image], type: item[type], deliveredPriceStr: item[deliveredPrice], orderID: item[orderId])
}
return results
}
现在我为Swift 2修改了它但无法管理;
static func findById(idToFind : Int64) -> T? {
let query = table.filter(id == idToFind)
do {
let query = try table.filter(id == idToFind)
let item = try SQLiteDataStore.sharedInstance.SADB.pluck(query)
if try item != nil {
let results : T = Payment(id: item[id], imageName: item[image], type: item[type], deliveredPriceStr: item[deliveredPrice], orderID: item[orderId])
} else {
print("item not found")
}
} catch {
print("delete failed: \(error)")
}
}
return results
}
我收到此错误:“无法下标Row类型的值”。我的项目的数据类型似乎已更改为Row。我该如何解析它?我该怎么办 ?
PS:我正在使用swift2分支。
答案 0 :(得分:3)
最后,我想出了如何获得价值。现在,行项目在swift-2分支上获取方法很简单。所以新方法是;
static func findById(idToFind : Int64) -> T? {
let query = table.filter(id == idToFind)
var results : T?
do {
let query = table.filter(id == idToFind)
let item = SQLiteDataStore.sharedInstance.SADB.pluck(query)
if try item != nil {
results = Payment( id: (item?.get(id))!, imageName: (item?.get(image))!, type: (item?.get(type))!, deliveredPriceStr: (item?.get(deliveredPrice))!, orderID: (item?.get(orderId))!)
} else {
print("item not found")
}
}
catch {
print("delete failed: \(error)")
}
return results
}
希望这有助于某人。