Notification mechanism simplified (swift)

The aim of this post is to explain and provide an example about notification mechanism simplified (,but not simplistic) in swift. The first thing that you need to be clear is when to use this mechanism and when delegate mechanism.

Both mechanism help to decouple view controllers. Notifications offer the advantage that multiple objects can observer one notifications.  In delegate mechanism the communication is direct (through a protocol method implementation) with the delegate class.

The example that I am going to present to shows the notification mechanism as a broadcast service. A timer periodically will launch a notification, on the other side there will be two classes (Alice and Bob) that will be intercepting the message.

As you can see in the code every 3 seconds is sent a notification that is intercepted by Alice and Bob class in the following way:

Is important to keep under control when to unsubscribe to the notification mechanism. In case of Alice is after receiving 3 messages and in case of Bob is when the class is removed from memory.

Once we run the project we should have to see the following in the log window:


You can find the sample project here.






Key-Value Observing mechanism simplified

The aim of this post is to show how it works Key-Value Observing mechanism (KVO). KVO is a mechanism for transparently notifying observers of changes in object properties. In other words,  this mechanism will allow us to trap when some object property value is being modified.


We will create a class that will hold the property to be observed:

Its implementation is the following:

As you can see the property is set to 3 in the class initialiser. Now we are going to implement the KVO mechanism in the ViewController. We will add the observer in the viewDidLoad Method:

As important as setup the observer is remove it when it is not necessary, I will add it in the dealloc method:

In the viewWillAppear method I will update property that is being observed:

The callback that is being called every time that the property is changed is the following:

When you will run the project you will see that the above function is called twice. Once during NewObject initialisation and the other during viewDidAppear.


And in the log screen:

You can download the code from here.



Singleton design pattern in a template

The aim of this post is to present Singleton design pattern in a template. I have seen some post and articles that showed the implementation key points separately, so this is why I have decided to write a mini-post showing an implementation template and its usage.

First of all, let’s define what a Singleton pattern is:

...the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects…. (Wikipedia)

Pattern implementation

This is my singleton implementation class:

I have added a local attribute (iLocalAttribute), for showing how to handle them, at the beginning I had to create them as static and for having access to them I had also to create static methods, but in a few lines I will explain how to avoid that.

The key point is the sharedInstance method:

  1. Declare a static variable to hold the instance of the class, ensuring it’s always available, but inside of  sharedInstance method context.
  2.  dispatch_once_t which ensures that the initialization code executes only once.
  3. Instance of MySingletonClass.
  4. Finally return the instance class.

Finally I have created a simple function that updates the local attribute and returns its value. As you can see neither the attribute nor the method are static.

This is the header class:


Pattern usage

This is my singleton implementation class: