Auto-Completion And Organization for NotificationCenter

I may be late to the party, but I just discovered the most amazing trick thanks to this article.

Have you seen this before? NSNotification.Name(rawValue: "didSomething"), object: nil)

… yea, its pretty ugly, not to mention incredibly error prone. If you have to use this same notification in multiple parts of your app, you are very likely to typo that string literal at some point.

But, I just discovered some syntax sugar to help clean that up.

extension NSNotification.Name {
    static let appDidSomething = NSNotification.Name(rawValue: "didSomething")

With that in place, you can start adjusting your call-sites like this. .appDidSomething, object: nil)

How glorious is that?

In retrospect, it seems a bit obvious, but the best tricks usually are.