Imbru Imbru - 15 days ago 6
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
selector:@selector(positionDidUpdate)
name:NMAPositioningManagerDidUpdatePositionNotification
object:[NMAPositioningManager sharedNMAPositioningManager]];


I have an error with the field option :


NavigationViewController.swift:30:84:
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

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())
Comments