在 Swift 3 中使用 -observeValueForKeyPath:ofObject:change:context:

2024-03-26

我正在尝试将 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

在我的函数上,其签名是:

func observeValue(forKeyPath keyPath: String?, ofObject: Any?, change: [String : Any]?, context: UnsafeMutableRawPointer?)."

我已阅读 Swift 3 迁移指南网站上的建议:

“解决方法:在使用原始 Objective-C 选择器实现可选需求之前添加 @objc(objectiveC:name:) 属性。”

但我不清楚这实际上应该如何应用在源文件中,而且到目前为止我所做的尝试都没有成功。

谁能解释如何正确设置它?


这就是我在 Xcode 版本 8.0 (8A218a) 中使用 Swift 3.0 的(最终)工作:

@objc
override open func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Swift 3 中使用 -observeValueForKeyPath:ofObject:change:context: 的相关文章

随机推荐