代码之家  ›  专栏  ›  技术社区  ›  Esqarrouth Jeremias Binder

Swift 4:无法将类型“(_)->Void”的值赋给类型“(()->())?”

  •  6
  • Esqarrouth Jeremias Binder  · 技术社区  · 7 年前

    XCode 9,Beta 3。Swift 4。

        let button = JumpingButton(x: 0, y: 50, w: 150, h: 300) // JumpingButton: UIButton
        //Inside JumpingButton: // var clickAction: (() -> ())?
    
    
        button.clickAction = { (sender) -> Void in //Error line
            action()
            Sound.playSound(Sounds.Button)
        }
    

    4 回复  |  直到 7 年前
        1
  •  8
  •   Code Different    7 年前

    因为 clickAction 需要一个无参数的函数/闭包。只需将代码更改为:

    button.clickAction = {
        action()
        Sound.playSound(Sounds.Button)
    }
    
        2
  •  3
  •   Alexander    7 年前

    我对这些函数的API一无所知(您从未告诉我们它们是什么),但下面是错误告诉您的:

    “(_)->无效'

    (_) -> ) Void .

    这是此参数所需的参数类型。它没有参数( () ),返回( ) ),并且是可选的( (...)?

    所以问题是,您正在将带有参数的闭包作为参数传递给期望无参数闭包的参数。

        3
  •  1
  •   Pablo Blanco    6 年前

    我有一个类似的问题,我这样解决:

    button.clickAction = { _ in
            action()
            Sound.playSound(Sounds.Button)
        }
    

    希望对你有帮助。

        4
  •  0
  •   zneak    7 年前