简体   繁体   English

Swift:缺少参数的参数

[英]Swift: Missing Argument for Parameter

1) I'm using a variable as the first argument in UIView.animateWithDuration like so: 1)我使用变量作为UIView.animateWithDuration的第一个参数, UIView.animateWithDuration所示:

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'. 和Xcode6(Beta 3)给我一个构建错误:'在调用中'参数'延迟'缺少参数'。

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: 2)或者,我可以跳过使用变量并包括在线计算:

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? 我在这做错了什么?

The error message is misleading. 错误消息具有误导性。 The first parameter of animateWithDuration() has the type NSTimeInterval (which is a Double ), but you pass a Float argument. animateWithDuration()的第一个参数的类型为NSTimeInterval (它是一个Double ),但是您传递了一个Float参数。 Swift does not implicitly convert types. Swift不会隐式转换类型。

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 Swift 3中,这将是

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

I also got this error from a totally unrelated error in the animations block; 我也从动画块中完全不相关的错误中得到了这个错误; I was already passing in a valid NSTimeInterval . 我已经传递了一个有效的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. 它与delay参数无关,所以它向我显示的错误是错误的并且让我在圈子中进行了一段时间。

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
        })

Actually Swift is typed language and it need to pass same type arguments as defined There is no implicit cast in swift. 实际上Swift是类型语言,它需要传递定义的相同类型的参数。在swift中没有隐式转换。

As animateWithDuration in decleration 作为animateWithDurationanimateWithDuration

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 具有NSTimeInterval参数类型,如果您看到它的声明,则声明为double

typealias NSTimeInterval = Double

So it need Double parameter value not Float value. 所以它需要Double参数值而不是Float值。 When you call second timeas in your code than swift is using type interfrence ie it is automatically defining(not convertting float to double) your indexPath.row * 1.0 to Double .The below code works fine. 当您在代码中调用第二个timeas而不是swift使用类型干涉时,它会自动定义(不将float转换为double)您的indexPath.row * 1.0Double 。下面的代码工作正常。

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 编译器误导你。因此总是传递与Swift定义的parmeter类型相同的参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM