How do I use BFTask with Swift 3?

1.1k views Asked by At

This segment of code worked perfectly in Swift 2

override func prepare(for segue: UIStoryboardSegue?, sender: Any?) {
    if (segue!.identifier == "FeedDetailSeg") {
        let viewController:ItemDetail = segue!.destination as! ItemDetail

        viewController.DetailItem = collectionItems![self.selectedRow]

        if self.cognitoID == collectionItems![self.selectedRow].seller {
            viewController.owned = true
        }
        else {
            viewController.owned = false
        }

        //remove item from untapped
        //appDelegate.untapped.removeAtIndex(self.selectedRow)

        //collect view info
        self.dataStash(collectionItems![self.selectedRow].ID, itemCondition: 2).continueWithBlock({
            (task: BFTask!) -> BFTask! in

            if (task.error != nil) {
                print(task.error!.description)
            } else {
                print("DynamoDB save succeeded")
            }

            return nil;
        })

    }
}

Now in Swift 3 I get an error on line

//collect view info    
self.dataStash(collectionItems![self.selectedRow].ID, itemCondition: 2).continueWithBlock({

that says: Cannot convert value of type '(BFTask<_>!) -> BFTask!' to expected argument type '(BFTask?) -> Any?!'

This is using AWS to save an item to DynamoDB.

2

There are 2 answers

0
Karthick Selvaraj On

Change the return type to Any? instead of BFTask? like this,

 //collect view info
    self.dataStash(collectionItems![self.selectedRow].ID, itemCondition: 2).continueWithBlock({
        (task: BFTask!) -> Any! in

        if (task.error != nil) {
            print(task.error!.description)
        } else {
            print("DynamoDB save succeeded")
        }

        return nil;
    })

Thanks:)

0
Wladek Surala On

(updated to Swift 3.1 syntax)

Your self.dataStash task specifies a type for generic BFTask in angle braces. Let's assume that your self.dataStash is of type:

var dataStash : BFTask<MyObject>!

then you have to change your code called on dataStash task completion to:

self.dataStash(collectionItems![self.selectedRow].ID, itemCondition: 2).continue({ (task: BFTask<MyObject>!) -> Any? in

especially in your chained task you can specify BFTask return type:

continue({ (task: BFTask<MyObject>!) -> BFTask<MyDifferentObject>? in

Note that MyObject and MyDifferentObject have to conform to AnyObject protocol.