在Swift中使用 - observeValueForKeyPath:ofObject:change:context: [英] Using - observeValueForKeyPath:ofObject:change:context: in Swift 3

查看:880
本文介绍了在Swift中使用 - observeValueForKeyPath:ofObject:change:context:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个Swift 2.2项目转换为Swift 3,我得到以下错误:

I'm trying to convert a Swift 2.2 project to Swift 3, and I'm getting the following error:

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

上使用相同的Objective-C选择器。 p>

On my function whose signature is:

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

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

I have read the advice on the Swift 3 Migration Guide web site:


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

"Workaround: Add an @objc(objectiveC:name:) attribute before the implementation of the optional requirement with the original Objective-C selector inside."

但是我不清楚这是真的应该在源文件中应用,我迄今为止没有尝试过的工作成功

But it isn't clear to me how this is actually supposed to be applied in a source file, and nothing I have tried so far has worked successfully.

任何人都可以解释如何正确设置此项。

Can anyone explain how to set this up properly?

推荐答案

刚开始键入 observeValueFor ... Xcode 会建议您有效的自动完成。

Just start typing observeValueFor... and Xcode will suggest you a valid autocompletion for that. Choose it, and you should be good to go.

您的函数应该看起来像这样:

Your function then should look something like this:

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) 

这篇关于在Swift中使用 - observeValueForKeyPath:ofObject:change:context:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆