无法分配类型 '() -> 的值Void' 输入 '(() -> Void)!' [英] Cannot assign value of type '() -> Void' to type '(() -> Void)!'

查看:17
本文介绍了无法分配类型 '() -> 的值Void' 输入 '(() -> 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆