title: Assigning values to properties in a singleton implemented with static let in Swift
body: <p>From reading it seems like the best advice for creating a singleton in Swift is to use <code>static let</code> like this:</p>
  
  <pre><code>struct GameManager {
    static let defaultManager = GameManager()
    var gameScore = 0
    var saveState = 0
    private init() {}
  }
  
  GameManager.defaultManager.gameScore = 1024 // Error
  GameManager.defaultManager.saveState = 12 // Error
  let score = GameManager.defaultManager.gameScore
  let savedProgress = GameManager.defaultManager.saveState
  </code></pre>
  
  <p>Because the <code>defaultManager</code> is declared as a constant (with "let"), there is an error when I try to assign <code>gameScore</code> and <code>saveState</code>.</p>
  
  <p><a href="https://i.stack.imgur.com/8LNw9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8LNw9.png" alt="playground error cannot assign to property"></a></p>
  
  <p>I'm running Xcode 7.0 beta 6 (7A192o) with Swift 2.0 (swiftlang-700.0.57.3 clang-700.0.72).</p>
  
  <p>If I change the <code>defaultManager</code> to be declared as a variable (with "var"), will it no longer be considered a proper singleton? </p>
  
  <hr>
  
  <p>If I change the GameManager to be declared as a class instead of a structure, then the code works as expected.</p>
  
  <pre><code>class GameManager {
    static let defaultManager = GameManager()
    var gameScore = 0
    var saveState = 0
    private init() {}
  }
  
  GameManager.defaultManager.gameScore = 1024 // No error, why?
  GameManager.defaultManager.saveState = 12 // No error, why?
  let score = GameManager.defaultManager.gameScore // 1,024
  let savedProgress = GameManager.defaultManager.saveState // 12
  </code></pre>
  
  <p>Can you explain why the reference-type class is better for implementing the singleton than the value-type structure?</p>