Ios swift 4.2无法转换类型为';(u)->;无效';到预期的参数类型';(()->;无效)';

Ios swift 4.2无法转换类型为';(u)->;无效';到预期的参数类型';(()->;无效)';,ios,swift,completion-block,Ios,Swift,Completion Block,==>swift 3版本运行良好,但swift 4和swift 4.2正在运行 static func animate(_ duration: TimeInterval, animations: (() -> Void)!, delay: TimeInterval = 0, options: UIViewAnimationOptions = [],

==>swift 3版本运行良好,但swift 4和swift 4.2正在运行

static func animate(_ duration: TimeInterval,
                    animations: (() -> Void)!,
                    delay: TimeInterval = 0,
                    options: UIViewAnimationOptions = [],
                    withComplection completion: (() -> Void)! = {}) {

    UIView.animate(
        withDuration: duration,
        delay: delay,
        options: options,
        animations: {
            animations()
        }, completion: { finished in
            completion()
    })
}

static func animateWithRepeatition(_ duration: TimeInterval,
                                   animations: (() -> Void)!,
                                   delay: TimeInterval = 0,
                                   options: UIViewAnimationOptions = [],
                                   withComplection completion: (() -> Void)! = {}) {

    var optionsWithRepeatition = options
    optionsWithRepeatition.insert([.autoreverse, .repeat])

    self.animate(
        duration,
        animations: {
            animations()
        },
        delay:  delay,
        options: optionsWithRepeatition,
        withComplection: { finished in
            completion()
    })
}
xcode上显示错误=>

无法将类型为“(\ux)->Void”的值转换为所需的参数类型 “(()->无效)?”


在函数声明中:

static func animate(_ duration: TimeInterval,
                    animations: (() -> Void)!,
                    delay: TimeInterval = 0,
                    options: UIViewAnimationOptions = [],
                    withComplection completion: (() -> Void)! = {})
您已将完成处理程序定义为
(()->Void),即它没有任何参数

但当您调用此函数时:

self.animate(
        duration,
        animations: {
            animations()
        },
        delay:  delay,
        options: optionsWithRepeatition,
        withComplection: { finished in
            completion()
    })

您正在完成块中提供参数
finished
。这就是为什么它给出了错误

您声明了
animate
函数,使其
completion
参数不接受任何输入参数。但是,当您在
animatewithrepeation
中调用该函数时,您正试图在闭包中调用输入参数
finished
。只要删除
finished
,您的代码就可以很好地编译

static func animateWithRepetition(_ duration: TimeInterval, animations: (() -> Void)!, delay: TimeInterval = 0, options: UIView.AnimationOptions = [], withComplection completion: (() -> Void)! = {}) {

    var optionsWithRepetition = options
    optionsWithRepeatition.insert([.autoreverse, .repeat])

    self.animate(duration, animations: {
        animations()
    }, delay: delay, options: optionsWithRepeatition, withCompletion: {
        completion()
    })
}

注:我已经纠正了输入参数名称中的错误。传递隐式展开类型的输入参数也没有多大意义。将
动画
设置为正常的
可选
并安全地将其打开,或者如果它永远不应该是
,则将其设置为非
可选
,哪一行?删除该行中的'finished in',因为
with completion
没有参数,所以没有“finished”。主题外,但有人能解释一下是什么!在参数声明
动画中表示:(()->Void)@JoakimDanielson查找隐式展开optionals@DávidPászor谢谢,当我将其用于
@IBOutlet
时,我确实理解了这个概念,但我并不真正理解在参数声明中包含它们意味着什么。@JoakimDanielson隐式展开的optionals(IUO)总是具有相同的含义。它们是可选值,但每当访问它们时,它们都将被强制展开以提供非可选值(实际上,在Swift 4.2中这有点改变,现在它们仅在需要非可选值时才被强制展开,否则它们不会被展开)。如果将函数输入参数声明为IUO,则无论何时使用,它都将在函数内部强制展开,但您仍然可以传入可选参数。这是否有真正的用例是另一个问题(对此我说不)。我正在删除
finished
here show me error不能将类型为“()->Void”的值转换为预期的参数类型“((Bool)->Void)“@Digvijay在
UIView
的扩展中声明两个函数时,这段代码对我来说编译得很好。你在哪里定义它们的?我正在我的项目中使用这个库,我正在获取这个问题@DávidPásztorbro我正在使用这个lib类和那个类来显示这个错误。@Digvijay那么你问题中的两个函数不是在你的代码中声明的,而是在链接库中声明的?如果是这样,您应该为库打开一个GitHub问题,让他们修复代码。如果这些函数是在代码中声明的,那么您需要用声明它们的位置更新问题。如前所述,将它们声明为
UIView
的扩展很好