我为绑定块创建了一个小协议(在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
}
}尝试继承此协议后,Xcode崩溃或编译错误,如Command failed due to signal: Segmentation fault: 11。
class View : UIView, BindingProtocol {
var binders: [String : (object: Any) -> Void]!
}有什么想法吗?
发布于 2016-03-16 11:32:24
你的财产没有问题,但是你的方法.
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点将其实现为变异方法。
它们有相同的签名,但实际上是两种不同的方法。在这种情况下,斯威夫特没有找到合适的电话。当我使用带有默认实现的协议时,这是一个常见的问题。
一个解决办法就是改变..。
protocol BindingProtocol {
...
//From
//func bind(string: String, block: storedClosure)
//To
mutating func bind(string: String, block: storedClosure)
}https://stackoverflow.com/questions/36034055
复制相似问题