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 gamemanager
s (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 gamemanager
s.
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
Post a Comment