Kevin ABRIOUX
Kevin ABRIOUX

Reputation: 17705

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

Upvotes: 3

Views: 6409

Answers (2)

thevikasnayak
thevikasnayak

Reputation: 629

UPDATED WITH SWIFT 5.4

NotificationCenter.default.addObserver(self,
                                       selector: #selector(doThisWhenNotify),
                                       name: NSNotification.Name("notificationKey"),
                                       object: nil)

Upvotes: 3

Octaviano Putra
Octaviano Putra

Reputation: 644

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

Upvotes: 4

Related Questions