Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using - observeValueForKeyPath:ofObject:change:context: in 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

On my function whose signature is:

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

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

"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?

like image 251
Cosmo Avatar asked Aug 25 '16 04:08

Cosmo


1 Answers

This is what (finally) worked for me using Swift 3.0 in Xcode Version 8.0 (8A218a):

@objc
override open func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
like image 121
Mark A. Durham Avatar answered Sep 19 '22 23:09

Mark A. Durham