Jguffey Jguffey - 26 days ago 6
iOS Question

Swift: Missing Argument for Parameter

1) I'm using a variable as the first argument in

UIView.animateWithDuration
like so:

var theDelay: Float = 1.0
UIView.animateWithDuration( theDelay, animations: {
cell.frame.origin.y = 0
})


and Xcode6 (Beta 3) is giving me a build error: 'Missing argument for parameter 'delay' in call'.

When I don't use a variable, the function works just fine. I'd like to tweak the variable (as this code is within a loop) when I discovered this issue.

2) Alternatively, I could skip using a variable and include the calculation in-line:

UIView.animateWithDuration( indexPath.row * 1.0, animations: {
cell.frame.origin.y = 0
})


but I am getting the same error 'Missing argument for parameter 'delay' in call'.

What am I doing wrong here?

Answer

The error message is misleading. The first parameter of animateWithDuration() has the type NSTimeInterval (which is a Double), but you pass a Float argument. Swift does not implicitly convert types.

Changing the variable definition to

let theDelay: NSTimeInterval = 1.0

or an explicit conversion

UIView.animateWithDuration( NSTimeInterval(theDelay), animations: {
    cell.frame.origin.y = 0
})

should solve the problem.

In Swift 3 this would be

let theDelay = TimeInterval(1.0)
UIView.animate(withDuration: theDelay, animations: {
    cell.frame.origin.y = 0
})