swift - Why am I allowed to create multiple instances of Singleton, even though its constructor is private? -


the code below singleton example book study swift. isn't aim of creating singletons having 1 object of type? in playground, i've been able create multiple gamemanagers (var = gamemanager(), var b = gamemanager() etc.)

by way aware there 1 defaultmanager object , cannot changed due being static , constant (let). couldn't use of private constructor since able create multiple gamemanagers.

class gamemanager {     static let defaultmanager = gamemanager()     var gamescore = 0     var savestate = 0     private init() {} } 

playground can access private constructor because swift lets access private long code in same file private code.

since typed directly playground editor's window considered single file, can access everywhere.

once put code outside playground, though, constructor's visibility enforced, preventing code creating instances of gamemanager intended.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -