jguffey
jguffey

Reputation: 466

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?

Upvotes: 5

Views: 11881

Answers (3)

Martin R
Martin R

Reputation: 539685

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 = 1.0

or an explicit conversion

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

should solve the problem.

In Swift 3 this would be

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

Upvotes: 9

sudo
sudo

Reputation: 5784

I also got this error from a totally unrelated error in the animations block; I was already passing in a valid NSTimeInterval. It had nothing to do with the delay parameter, so the error it was showing me was wrong and had me going in circles for a while.

So make sure you don't have any errors inside the animations block.

UIView.animateWithDuration(interval, animations: { () -> Void in // compiler will complain about this line missing a parameter
            // some code
            // some code with a syntax error in it, but Xcode won't show the error
            // some code
        })

Upvotes: 3

codester
codester

Reputation: 37189

Actually Swift is typed language and it need to pass same type arguments as defined There is no implicit cast in swift.

As animateWithDurationin decleration

class func animateWithDuration(duration: NSTimeInterval, animations: (() -> Void)!) // delay = 0.0, options = 0, completion = NULL

has parameter type of NSTimeInterval which is declared as double if you see its declaration

typealias NSTimeInterval = Double

So it need Double parameter value not Float value. When you call second timeas in your code than swift is using type interfrence i.e it is automatically defining(not convertting float to double) your indexPath.row * 1.0 to Double.The below code works fine.

var theDelay: NSTimeInterval = 1.0 

or

var theDelay: Double = 1.0  //this is same as above
UIView.animateWithDuration( theDelay, animations: {
    cell.frame.origin.y = 0
})

Compiler is misguiding you.So always pass parmeter type same as defined in Swift

Upvotes: 1

Related Questions