Singleton dessing pattern in a template (Swift)

The aim of this post is just complement Singleton design pattern in a template post , but in Swift.

Singleton implementation class

import UIKit

class MySingletonClass: NSObject {
   static let sharedInstance = MySingletonClass()
    override init() {
    func giveMe5()->Int{
        return 5


Not that in Swift ‘dispatch_once’ is no longer necessary, as in Objective-C. Static constants in Swift, internally does it. So this is an issue that you do not have to worry about.

Template usage

This is how the pattern is used.




As you can see Swift language structure facilitates a lot Singleton design pattern implementation. You can get the project source code used for writing this post here.

Leave a Reply

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

three × two =