Implementation

Implementation of the Singleton pattern often typically creates a single object using the factory method, and this instance/object is called a shared instance in most cases. Since the access to the instance is passed on through a class method, the need to create an object is eliminated. Let's look at the Singleton implementation in the code in the following section.

For this exercise, we will use the command line tool Xcode template to create a project and name it Singleton. Our Singleton class will be called SingletonObject, which we will create as a normal Cocoa class, and it will be a subclass of NSObject. The project setup will look like this so far:

Let's add a class method called sharedInstance as discussed earlier, ...

Get Reactive Programming with Swift 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.