ios - swift 4.2 无法将类型“(_)-> Void”的值转换为预期的参数类型“(()-> Void)?”
问题描述
==> 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 上的错误显示 =>
无法将“(_) -> Void”类型的值转换为预期的参数类型“(() -> Void)?”
解决方案
您声明了animate
函数,使其completion
参数不接受输入参数。但是,finished
当您在animateWithRepetition
. 只需删除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()
})
}
PS:我已经更正了您输入参数名称中的拼写错误。传入隐式展开类型的输入参数也没有多大意义。要么animations
正常Optional
并安全地打开它,要么让它成为非Optional
,如果它不应该是nil
。
推荐阅读
- curl - 如何修复curl的证书位置
- python-3.x - python子进程不继承虚拟环境
- anaconda - Anaconda 在中国的可用性
- ios - 用于视频通话的 iOS PIP(画中画)是私有 API 吗?
- java - 有没有办法从只有一个框架矩形中制作多个未连接的矩形(在整个框架中有多个位置)?
- java - 如何获得三个并行数组相加的最大值的索引?
- javascript - 如何比较 Google App Scripts 中的字符串
- css - IE11 中的 CSS 缩放/变换行为
- python - 使用 cv2 混合 2 个图像时如何去除光影边框?
- ruby-on-rails - 设计:没有用户登录时的自定义重定向