使用'ReferenceWritableKeyPath'的Swift编译器分段错误`
使用使用'ReferenceWritableKeyPath'的Swift编译器分段错误`,swift,observable,swift4,Swift,Observable,Swift4,使用referenceWriteKeyPath读取属性会导致编译器分段错误 protocol Bindable: class { var observers: [NSKeyValueObservation] {get set} } extension Bindable { func bind<Value>(to targetKeyPath: ReferenceWritableKeyPath<Self, Value>, from sourceKeyPath
referenceWriteKeyPath
读取属性会导致编译器分段错误
protocol Bindable: class {
var observers: [NSKeyValueObservation] {get set}
}
extension Bindable {
func bind<Value>(to targetKeyPath: ReferenceWritableKeyPath<Self, Value>, from sourceKeyPath: KeyPath<Self, Value>)
where Self: NSObject {
self.observers.append( self.observe(sourceKeyPath, options: [.initial, .new]) {object, change in
// FAILS: compiler failed due to signal: Segmentation fault: 11
if( self[keyPath:targetKeyPath] != change.newValue ) {
self[keyPath: targetKeyPath] = change.newValue!
}
})
}
}
我正在设置一个助手来简化绑定两个变量。与我得到的基本绑定工作,但如果修改代码如下所示,检查值是不同的,然后再进行绑定,它有一个分段错误
protocol Bindable: class {
var observers: [NSKeyValueObservation] {get set}
}
extension Bindable {
func bind<Value>(to targetKeyPath: ReferenceWritableKeyPath<Self, Value>, from sourceKeyPath: KeyPath<Self, Value>)
where Self: NSObject {
self.observers.append( self.observe(sourceKeyPath, options: [.initial, .new]) {object, change in
// FAILS: compiler failed due to signal: Segmentation fault: 11
if( self[keyPath:targetKeyPath] != change.newValue ) {
self[keyPath: targetKeyPath] = change.newValue!
}
})
}
}
协议可绑定:类{
变量观察者:[NSKeyValueObservation]{get set}
}
可扩展绑定{
func绑定(到targetKeyPath:ReferenceWritableKeyPath,来自sourceKeyPath:KeyPath)
where Self:NSObject{
self.observators.append(self.observate(sourceKeyPath,选项:[.initial,.new]){object,中的更改
//失败:由于信号:分段错误:11,编译器失败
if(self[keyPath:targetKeyPath]!=change.newValue){
self[keyPath:targetKeyPath]=change.newValue!
}
})
}
}
问题在于您试图使用=代码>具有泛型类型值
,不一定具有==
和=代码>实现。用
替换
可以解决此问题
话虽如此,编译器因分段错误而崩溃始终是一个bug,不管您的代码是否正确。如果你有时间,你应该考虑提交一个bug报告。我试过了,但是String不是协议或类,所以不起作用。我需要的是平等。我提出了一个分段错误的建议:SR-5375