Imbru Imbru - 1 year ago 118
Objective-C Question

Cannot convert value of type 'NSNotification.Name' to expected argument type 'NSKeyValueObservingOptions'

In NMAPositioningManager.h there is this const :

FOUNDATION_EXPORT NSString *const NMAPositioningManagerDidUpdatePositionNotification;

And there is my code in swift

NotificationCenter.addObserver(self, forKeyPath: "positionDidUpdate", options: NSNotification.Name.NMAPositioningManagerDidUpdatePosition, context: NMAPositioningManager.shared())

Inspired from this example in Obj-C:

[[NSNotificationCenter defaultCenter] addObserver:self
object:[NMAPositioningManager sharedNMAPositioningManager]];

I have an error with the field option :

Cannot convert value of type 'NSNotification.Name' to expected
argument type 'NSKeyValueObservingOptions'

enter image description here

What i have to type to have my Swift Code working ?

EDIT : using NotificationCenter instead of Notification

Answer Source

You should call the addObserver.. method on default singleton It should be:

NotificationCenter.default.addObserver(self, selector: #selector(positionDidUpdate), name: NSNotification.Name.NMAPositioningManagerDidUpdatePosition, object: NMAPositioningManager.shared())
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download