Singleton with properties in Swift 3

SingletonSwift3

Singleton Problem Overview


In Apple's Using Swift with Cocoa and Objective-C document (updated for Swift 3) they give the following example of the Singleton pattern:

class Singleton {
    static let sharedInstance: Singleton = {
        let instance = Singleton()

        // setup code

        return instance
    }()
}

Let's imagine that this singleton needs to manage a variable array of Strings. How/where would I declare that property and ensure it gets initialized properly to an empty [String] array?

Singleton Solutions


Solution 1 - Singleton

For me this is the best way, make init private. Swift 3 \ 4 \ 5 syntax

// MARK: - Singleton

final class Singleton {
    
    // Can't init is singleton
    private init() { }
    
    // MARK: Shared Instance
    
    static let shared = Singleton()
    
    // MARK: Local Variable
    
    var emptyStringArray = [String]()
    
}

Solution 2 - Singleton

You can initialize an empty array like this.

class Singleton {
    
    //MARK: Shared Instance
    
    static let sharedInstance : Singleton = {
        let instance = Singleton(array: [])
        return instance
    }()
    
    //MARK: Local Variable

    var emptyStringArray : [String]
    
    //MARK: Init

    init( array : [String]) {
        emptyStringArray = array
    }
}

Or if you prefer a different approach, this one will do fine.

class Singleton {
    
    //MARK: Shared Instance
    
    static let sharedInstance : Singleton = {
        let instance = Singleton()
        return instance
    }()
    
    //MARK: Local Variable

    var emptyStringArray : [String]? = nil
    
    //MARK: Init

    convenience init() {
        self.init(array : [])
    }
    
    //MARK: Init Array

    init( array : [String]) {
        emptyStringArray = array
    }
}

Solution 3 - Singleton

As per the apple's documentation: In Swift, you can simply use a static type property, which is guaranteed to be lazily initialized only once, even when accessed across multiple threads simultaneously.

class Singleton {

    // MARK: - Shared

    static let shared = Singleton()
}

With initialization method:

class Singleton {
    
    // MARK: - Shared
    
    static let shared = Singleton()
    
    // MARK: - Initializer
    
    private init() {
    }
    
}

Solution 4 - Singleton

Any initialisation would be done in an init method. No difference here between a singleton and a non-singleton.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobertJosephView Question on Stackoverflow
Solution 1 - SingletonYannStephView Answer on Stackoverflow
Solution 2 - Singletonuser6375148View Answer on Stackoverflow
Solution 3 - SingletonMehul SojitraView Answer on Stackoverflow
Solution 4 - Singletongnasher729View Answer on Stackoverflow