Singleton z nieruchomościami w Swift 3

W języku Apple używając Swift z Cocoa i Objective-C document (zaktualizowany dla Swift 3) podają następujący przykład wzorca Singletona:

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

        // setup code

        return instance
    }()
}

Wyobraźmy sobie, że ten singleton musi zarządzać zmienną tablicą łańcuchów. Jak / Gdzie mogę zadeklarować tę właściwość i upewnić się, że zostanie poprawnie zainicjalizowana do pustej tablicy [String]?

Author: RobertJoseph, 2016-06-21

4 answers

Dla mnie jest to najlepszy sposób, aby init był prywatny. Swift 3 / 4 składnia

// 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]()

}
 225
Author: YannickSteph,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-19 14:27:23

Możesz zainicjalizować pustą tablicę w ten sposób.

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
    }
}

Lub jeśli wolisz inne podejście, to będzie dobrze.

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
    }
}
 55
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-21 20:33:19

Zgodnie z dokumentacją apple: w języku Swift możesz po prostu użyć właściwości statycznej, która z pewnością zostanie zainicjowana tylko raz, nawet gdy dostęp do niej będzie możliwy w wielu wątkach jednocześnie .

class Singleton {

    // MARK: - Shared Instance

    static let sharedInstance = Singleton()
}

Lub, z metodą inicjalizacji:

class Singleton: NSObject {

    // MARK: - Shared Instance

    static let sharedInstance: Singleton = {
        let instance = Singleton()
        // setup code
        return instance
    }()

    // MARK: - Initialization Method

    override init() {
        super.init()
    }
}
 26
Author: Mehul Sojitra,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-23 05:51:49

Inicjalizacja odbywa się metodą init. Nie ma różnicy między singletonem a nie singletonem.

 1
Author: gnasher729,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-21 19:51:33