0

I got an error while using blocks in animateWithDuration:animations:completion: method

below is my code:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var cell=tableView.cellForRowAtIndexPath(indexPath)
    var backGrndView:UIView?=cell?.contentView.viewWithTag(2) as UIView?
    UIView.animateWithDuration(0.2,
        animations: {
            backGrndView?.backgroundColor=UIColor.redColor()
        },
        completion: { finished in
            backGrndView?.backgroundColor=UIColor.redColor()
    })
}

I tried the solutions at link.

But my problem is not solved.

below is screenshot:

enter image description here

Please help me out.

Thanks in advance

Let's_Create
  • 2,963
  • 3
  • 14
  • 33
Ashok
  • 5,585
  • 5
  • 52
  • 80

1 Answers1

3

Seems that problem is the fact that swift auto returns if there is only one statement in closure. You can work around this by adding explicit returns:

UIView.animate(withDuration: 0.2,
                           animations: {
                            backGrndView?.backgroundColor = UIColor.red
                            return
},
                           completion: { finished in
                            backGrndView?.backgroundColor = UIColor.red
                            return
})

This statement:

 backGrndView?.backgroundColor = UIColor.red

returns ()? that is same as Void?, but closure return type is Void.

Karen Hovhannisyan
  • 1,140
  • 2
  • 21
  • 31
Kirsteins
  • 27,065
  • 8
  • 76
  • 78