Reputation: 556
I am really struggling to understand the new syntax for observers.
Can you help me translate this to Swift 3.
nc.addObserver(self, selector: #selector(MapViewController.locationUpdated(_:)), name: LocationNotification.kLocationUpdated, object: nil)
nc.addObserver(self, selector: #selector(MapViewController.locationAuthorizationStatusChanged(_:)), name: LocationNotification.kAuthorizationStatusChanged, object: nil)
nc.addObserver(self, selector: #selector(MapViewController.locationManagerDidFailWithError(_:)), name: LocationNotification.kLocationManagerDidFailWithError, object: nil)
Many thanks!
Upvotes: 1
Views: 2856
Reputation: 419
Remember to make the method accepting the notifications public (if its on a different controller).
And you should also add the processor tag objc
so objective-c methods can call it.
Assign Observers:
nc.addObserver(
self,
selector: #selector(received(notification:)),
name: LocationNotification.kLocationUpdated, object: nil
)
Handle notifications:
@objc public func locationUpdated(notification:Notification) {
//Do something
}
Hope this helps! :-)
Upvotes: 1
Reputation: 9777
The syntax of your code is valid for Swift 3. With this syntax, I am assuming your LocationNotification
object looks something like this:
struct LocationNotification {
static let kLocationUpdated = Notification.Name(rawValue: "LocationUpdated")
static let kAuthorizationStatusChanged = Notification.Name(rawValue: "AuthorizationStatusChanged")
static let kLocationManagerDidFailWithError = Notification.Name(rawValue: "LocationManagerDidFailWithError")
}
Upvotes: 1