1

This method was working while I was using Swift 1.2. But now, I had to update to Xcode and I had to switch my language to Swift 2. This is the method from swift 1.2 which I used well ;

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

}

Now I modified it for Swift 2 but couldn't manage;

 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

    }

And I'm getting this error : "Cannot subscript a value of type Row" . My item's data type seems like changed to Row. How can I parse it ? What should I do ?

PS: I'm using swift2 branch.

letitbefornow
  • 427
  • 6
  • 20

1 Answers1

4

Finally I figured out how to get values. It's easy now the row item has get method on swift-2 branch. So new method is ;

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

}

Hope that this helps someone.

letitbefornow
  • 427
  • 6
  • 20