无法分配类型 '() -> 的值Void' 输入 '(() -> Void)!' [英] Cannot assign value of type '() -> Void' to type '(() -> Void)!'
问题描述
更新到 xcode 8 beta 6 后,我收到错误
<块引用>无法将类型 '() -> Void' 的值赋给类型 '(() -> Void)!'
在下面的函数块中,第三行:
//按钮子类公共类 SCLButton: UIButton {var actionType = SCLActionType.nonevar 目标:AnyObject!var 选择器:选择器!var action:(()->Void)!公共初始化(){super.init(frame: CGRect.zero)}需要公共初始化?(编码器aDecoder:NSCoder){super.init(编码器:aDecoder)}覆盖公共初始化(框架:CGRect){super.init(框架:框架)}//需要 public init?(coder aDecoder: NSCoder) {//fatalError("init(coder:)还没有实现")//}}public func addButton(_ title:String, action:()->Void)->SCLButton {让 btn = addButton(title)btn.actionType = SCLActionType.closurebtn.action = action//这里是错误发生的地方btn.addTarget(self, action:#selector(SCLAlertView.buttonTapped(_:)), for:.touchUpInside)btn.addTarget(self, action:#selector(SCLAlertView.buttonTapDown(_:)), for:[.touchDown, .touchDragEnter])btn.addTarget(self, action:#selector(SCLAlertView.buttonRelease(_:)), for:[.touchUpInside, .touchUpOutside, .touchCancel, .touchDragOutside] )返回 btn}
关于修复的任何建议?
您的问题似乎与此有关:SE-0103>
尝试将 addButton(_:action:)
的方法标题更改为:
public func addButton(_ title:String, action:@escaping()->Void)->SCLButton {
来自新测试版的诊断信息像往常一样令人困惑和不足,但让您的属性简单地成为非可选 var action:()->Void = {}
会给您更多有用的信息.
after updating to xcode 8 beta 6, I'm getting the error
Cannot assign value of type '() -> Void' to type '(() -> Void)!'
on the following func block, third line:
// Button sub-class
public class SCLButton: UIButton {
var actionType = SCLActionType.none
var target:AnyObject!
var selector:Selector!
var action:(()->Void)!
public init() {
super.init(frame: CGRect.zero)
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder:aDecoder)
}
override public init(frame:CGRect) {
super.init(frame:frame)
}
// required public init?(coder aDecoder: NSCoder) {
// fatalError("init(coder:) has not been implemented")
// }
}
public func addButton(_ title:String, action:()->Void)->SCLButton {
let btn = addButton(title)
btn.actionType = SCLActionType.closure
btn.action = action // here is where the error occurs
btn.addTarget(self, action:#selector(SCLAlertView.buttonTapped(_:)), for:.touchUpInside)
btn.addTarget(self, action:#selector(SCLAlertView.buttonTapDown(_:)), for:[.touchDown, .touchDragEnter])
btn.addTarget(self, action:#selector(SCLAlertView.buttonRelease(_:)), for:[.touchUpInside, .touchUpOutside, .touchCancel, .touchDragOutside] )
return btn
}
any suggestions on a fix?
It seems your issue is related to this: SE-0103
Try chaging the method header of your addButton(_:action:)
to:
public func addButton(_ title:String, action:@escaping ()->Void)->SCLButton {
The diagnostic messages from new betas are so confusing and inadequate as usual, but making your property simply non-Optional var action:()->Void = {}
will give you a little more useful info.
这篇关于无法分配类型 '() -> 的值Void' 输入 '(() -> Void)!'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!