Swift Singleton

If you’re interested in a singleton pattern for Swift, consider this solution. Personally I find singleton classes something that can easily be avoided and their intent should be obvious. Just implement it once on your own. If someone decides to implement something that blows things up if they instantiate more than one, that seems to be a different problem.

class Utility {
    static let sharedInstance = Utility()
    private init() {} 
    // private initializer of the class will restrict 
    // others from using the default initializer.
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.