Swift Singleton

Sample with dispatch_once

class SingletonTest {
    
    class var sharedInstance : SingletonTest {
    
        struct Static {
            static var onceToken : dispatch_once_t = 0
            static var instance : SingletonTest? = nil
        }
        
        dispatch_once(&Static.onceToken) {
            Static.instance = SingletonTest()
        }
        return Static.instance!
    }
    
}

Use

var singleton = SingletonTest.sharedInstance

Ref

hpique/SwiftSingleton