gpt4 book ai didi

swift - 无法继承我的协议(protocol)(命令因信号 : Segmentation fault: 11) 而失败

转载 作者:搜寻专家 更新时间:2023-11-01 06:40:30 25 4
gpt4 key购买 nike

我为绑定(bind) block 创建了一个小协议(protocol)(它是 Swift 中 KVO 的一些循环)代码在这里:

typealias storedClosure = (object: Any) -> Void

protocol BindingProtocol {
var binders: [String : storedClosure]! { get set }
func bind(string: String, block: storedClosure)
}

extension BindingProtocol {
mutating func bind(string: String, block: storedClosure) {
if binders == nil {
binders = [String : storedClosure]()
}
binders[string] = block
}
}

在我尝试继承此协议(protocol)后,我遇到 Xcode 崩溃或编译错误,例如 Command failed due to signal: Segmentation fault: 11

class View : UIView, BindingProtocol {
var binders: [String : (object: Any) -> Void]!
}

有什么想法吗?

最佳答案

你的属性没有问题,但是你的方法...

protocol BindingProtocol {
var binders: [String : storedClosure]! { get set }
func bind(string: String, block: storedClosure) //<--- 1.This
}


extension BindingProtocol {
mutating func bind(string: String, block: storedClosure) { //<--- 2.This
if binders == nil {
binders = [String : storedClosure]()
}
binders[string] = block
}
}

您在第 1 点将方法定义为普通方法,并在第 2 点将其实现为变异。

它们具有相同的签名,但实际上是两种不同的方法。在这种情况下,Swift 没有找到合适的调用方法。这是我在使用默认实现的协议(protocol)时遇到的一个常见问题。

一个解决方案就是改变...

protocol BindingProtocol {
...
//From
//func bind(string: String, block: storedClosure)

//To
mutating func bind(string: String, block: storedClosure)
}

关于swift - 无法继承我的协议(protocol)(命令因信号 : Segmentation fault: 11) 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36034055/

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