gpt4 book ai didi

ios - swift 4.2 无法将类型 '(_) -> Void' 的值转换为预期的参数类型 '(() -> Void)?'

转载 作者:搜寻专家 更新时间:2023-11-01 06:54:36 27 4
gpt4 key购买 nike

==> 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 上的错误显示=>

Cannot convert value of type '(_) -> Void' to expected argument type '(() -> Void)?'

最佳答案

您声明了 animate 函数,使其 completion 参数不接受任何输入参数。但是,当您在 animateWithRepetition 中调用该函数时,您正试图在闭包中调用输入参数 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()
})
}

P.S.:我已经更正了您输入的参数名称中的拼写错误。传入隐式解包类型的输入参数也没有多大意义。要么将 animations 设置为普通的 Optional 并安全地打开它,要么将其设置为非 Optional 如果它永远不应该是 nil.

关于ios - swift 4.2 无法将类型 '(_) -> Void' 的值转换为预期的参数类型 '(() -> Void)?',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54420159/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com