gpt4 book ai didi

swift - 无法调用非函数类型的值 'Closure Name'

转载 作者:行者123 更新时间:2023-11-28 14:13:13 26 4
gpt4 key购买 nike

我定义了一个名为 touchBlock 的闭包,并在 override func touchesEnded 中使用它,然后我得到一个错误:

Cannot call value of non-function type '((ELControl, UIEvent, UIEvent) -> Void?)?

这是我的代码:

var touchBlock: ((_ view: ELControl, _ touches: UIEvent, _ event: UIEvent) -> Void?)?    
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

if self.touchBlock != nil {

self.touchBlock(nil, touches, event)
} else {

super.touchesEnded(touches, with: event)
}
}

请帮我修复它。

最佳答案

你必须打开可选的

self.touchBlock!(nil, touches, event)

或者最好有可选的绑定(bind)

if let block = self.touchBlock {
block(nil, touches, event) ...

但是考虑到传递的参数根本不匹配闭包的类型(viewevent 是非可选的并且 touches 的类型 完全不同)。

关于swift - 无法调用非函数类型的值 'Closure Name',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52362340/

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