For a class, each notification observation should be handled by a single method. The name of that method should be handle
appended with the name of the notification being observed. For instance, for notification name
NSNotification.Name.LUUICachedPaymentMethodDidUpdate
the handler would be
private func handleCachedPaymentMethodDidUpdate(...)
Skip whatever LU/LUUI prefixes exist.
Only the notification observation should call/reference the handler method.
Here's how the resultant code would look:
// somewhere
NotificationCenter.default.addObserver(self, selector: #selector(handleCachedPaymentMethodDidUpdate),
name: NSNotification.Name.LUUICachedPaymentMethodDidUpdate,
object: nil)
// later
private func handleCachedPaymentMethodDidUpdate(_ notification: NSNotification) {
// 🧹 handle it!
}
All notification handler methods should be private, and should be grouped together in a section delineated by
// MARK: - Notification handlers
When trying to comprehend code, especially when debugging, it can become confusing to have a method that is called by:
- multiple notifications
- both by a notification and direct calls
Breaking out notification handlers on their own is a simple, consistent way to structure this for clarity.