使用 ReferenceWritaleKeyPath
读取属性导致编译器段错误。
我正在设置一个助手来简化两个变量的绑定(bind)。用help from OOPer我的基本绑定(bind)可以正常工作,但是如果如下所示修改代码以在执行绑定(bind)之前检查值是否不同,则会出现段错误。
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!
}
})
}
}
最佳答案
问题是您尝试使用 !=
使用通用类型 Value
,不一定有 ==
和 !=
实现。替换 <Value>
通过 <Value: Equatable>
解决它。
话虽如此,无论您的代码是否正确,因段错误而崩溃的编译器始终是一个错误。您应该考虑在 https://bugs.swift.org 提交错误报告如果你有时间。
关于使用 `ReferenceWritableKeyPath ` 的 Swift 编译器段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44917650/