Implement a singleton class

 Problem

I want to create a Singleton class.



 Recipe


// Creating our Singleton

class SharedManager {
    // Declare our 'sharedInstance' property
    static let sharedInstance = SomeManager()

    // Set an initializer - 
    // it will only be called once
    init() {
        print("SomeManager initialized")
    }

    // Add a test function
    func doSth() {
        print("I'm doing something")
    }
}

// The 'init' function will 
// only be called the first time
SharedManager.sharedInstance.doSth()
SharedManager.sharedInstance.doSth()

				

Heard the news?

The Swift Cookbook for Swift 3 is at last here! For iBooks, Kindle and Paperback.
Hurry up - and Check it out! ;-)

 

 Playground