Swift Singleton

Just read through a forum post on creating a singleton with Swift, which came up with a couple of what seemed to me to be less than optimal options .  I reckon the best way is to just declare your class with a private constructor and then have a static property that returns the instance…

class Lonely {
  private init() {
  }

static let instance = Lonely()
}

let myLonely = Lonely() // won’t work because there are no internal/public initializers available

let myLonely = Lonely.Instance // gives you the only possible instance

This is pretty much the standard approach in C# and it seems to work fine in Swift (2.0 at least, haven’t tested it with earlier versions)