Post your changes with NotificationCenter

Rajai Kumar
Nerd For Tech
Published in
3 min readAug 9, 2022

--

created by kjpargeter

Hello devs,

There are many ways to use NotificationCenter and I just want to show you something which you may already know. We are trying to notify changes/updation and trigger an action based on it.

Let’s jump to code now…

Step 1:

Open a fresh playground for this.

We are going to create a class named car with some properties.

ray.so

Step 2:

Write the initialisers for those properties.

ray.so

You may wonder where did those Brands and Fuel types came from. Don’t worry I’ll provide that too.

ray.so

Step 4:

We need some action as a result. So we are going to create function to print a statement with the data we are going to collect.

Step 5:

We need to create a notification which represents the data change.

Step 6:

Now create a method to add observer. With the help of NotificationCenter we need to create an observer with the notification we created earlier. And the print statement function will be the action triggered. Call this method in the initialiser.

ray.so

Step 7:

Let’s create an object of the class Car with data.

Step 8:

Finally post the carChangeNotification notification to trigger the print action.

That’s it guys, it’s time for me to leave. Have the full code given below.

👾 Gist

🙏🏽 Thanks- ray.so, Swift Goose

--

--

Rajai Kumar
Nerd For Tech

I’m a workaholic iOS developer. I focus a lot on UX and product efficiancy.