Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Missing Argument for Parameter

Tags:

ios

swift

xcode6

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?

like image 921
jguffey Avatar asked Jul 17 '14 17:07

jguffey


3 Answers

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
})
like image 151
Martin R Avatar answered Oct 18 '22 20:10

Martin R


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
        })
like image 24
sudo Avatar answered Oct 18 '22 19:10

sudo


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

like image 1
codester Avatar answered Oct 18 '22 19:10

codester