Implementing a Singleton Class in Swift

The Singleton design pattern guarantees that only on object of a class will exist in a system.

// Creating Singleton
// Only one instance of this class can be created
class CloudCodeExecutor {

    // Declare class instance property
    static let sharedInstance = CloudCodeExecutor()
    // Declare an initializer 
    // Because this class is singleton only one instance of this class can be created
    init() {
        print("CloudCodeExecutor has been initialized")

    // Add a function
    func processCloudCodeOperation() {
        print("Started processing cloud code operation")

        // Your other code here

// Call function of Singleton class

// Call cloud code operation function again

// And again to see that class initializer was called only once

[raw_html_snippet id=”cookbookpagecoursesheader”]

Unit Testing Swift Mobile App

Apply Test-Driven Development(TDD) process to iOS mobile app development in Swift Preview this video course.