A way to handle NSNotification’s observation token

If you are working with closure based NSNotification’s implementation, you have to deal with returned `tokens`. The simplest way to store reference to them is to keep them in a property:

Situation complicates when we have to store more than one token. 
We can solve it by keeping references in the array:

If we wouldn’t like to create temporary constants willEnterForegroundToken didBecomeActiveToken, there are at least two ways to solve it.

Let’s start with more obvious one:

I leave it to you to judge this implementation. 
I would like to share with you another way to handle it. First step is to introduce ReferencesBag:

It’s a class inspired by Disposables concept which I got know when I was working with RxSwift.

Next step is to extend NSObjectProtocol protocol:

Having those two pieces we can write something like this:

Which one is the best? It depends on your taste. I just wanted to present you an alternative approach.

My github: https://github.com/adiki/

Next article: Static let pattern
Previous article: Wrapper design pattern by example using SFAuthenticationSession and ASWebAuthenticationSession