Singleton in swift


Examples of singletons

class SingletonA {
    static let sharedInstance = SingletonA()
    init() {
        println("AAA");
    }
}

class TheOneAndOnlyKraken {
    static let sharedInstance = TheOneAndOnlyKraken()
    private init() {} //This prevents others from using the default '()' initializer for this class.
	func test(){
		print("hello world")
	}
}


let singleton = TheOneAndOnlyKraken.sharedInstance
singleton.test()

Another singleton example:

// Creating our Singleton

class Manager {
    // Declare our 'sharedI' property
    static let shared = 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
Manager.shared.doSth()