使用 – observeValueForKeyPath:ofObject:change:context:在Swift 3中
我试图将Swift 2.2项目转换为Swift 3,我得到以下错误:
Method 'observeValue(forKeyPath:ofObject:change:context:)' with Objective-C selector 'observeValueForKeyPath:ofObject:change:context:' conflicts with method 'observeValue(forKeyPath:of:change:context:)' from superclass 'NSObject' with the same Objective-C selector" for the function call
在我的function上签名是:
func observeValue(forKeyPath keyPath: String?, ofObject: Any?, change: [String : Any]?, context: UnsafeMutableRawPointer?)."
我已阅读Swift 3 Migration Guide网站上的build议:
“解决方法:在使用最初的Objective-Cselect器实现可选需求之前添加@objc(objectiveC:name :)属性。
但是我不清楚这实际上是应该在源文件中应用的,到目前为止我没有尝试过。
任何人都可以解释如何正确设置?
只要开始键入observeValueFor...
和Xcode
会build议你一个有效的自动完成。 select它,你应该很好走。
你的function应该如下所示:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)
这是什么(终于)为我在Xcode版本8.0(8A218a)使用Swift 3.0:
@objc override open func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {