Chapter 9: onPost() func and closure

Hi,

I don’t quite understand how the func post() is structured in Chapter 9. Can someone please explain how the “using” closure will be called in the viewDidLoaded() l later?

“extension Notification.Name {
// 1
static let acceptButton = Notification.Name(“acceptTapped”)
static let rejectButton = Notification.Name(“rejectTapped”)

// 2
func post(center: NotificationCenter = NotificationCenter.default,
object: Any? = nil,
userInfo: [AnyHashable : Any]? = nil) {
center.post(name: self, object: object, userInfo: userInfo)
}

// 3
@discardableResult
func onPost(center: NotificationCenter = NotificationCenter.default,
object: Any? = nil,
queue: OperationQueue? = nil,
using: @escaping (Notification) → Void)
→ NSObjectProtocol {
return center.addObserver(forName: self, object: object,
queue: queue, using: using)
}
}”

Thanks!
Mike

I’m not entirely sure what you’re asking. The onPost is the method called when the notification is posted. When the notification gets posted, then the closure you provide gets called. So when somebody does Notification.Name.acceptButton.post() for example, then the closure specified at Notification.Name.acceptButton.onPost will be run.