title: 'dispatch_once_t' is unavailable in Swift: Use lazily initialized globals instead
body: <p>I am having trouble with <code>dispatch_once_t</code> when migrating to Swift 3.</p>
  
  <p>According to <a href="https://swift.org/migration-guide/">Apple's migration guide</a>:</p>
  
  <blockquote>
    <p>The free function dispatch_once is no longer available in Swift. In
    Swift, you can use lazily initialized globals or static properties and
    get the same thread-safety and called-once guarantees as dispatch_once
    provided. Example:</p>
    
    <p><code>let myGlobal = { … global contains initialization in a call to a closure … }()</code></p>
    
    <p><code>_ = myGlobal  // using myGlobal will invoke the initialization code only the first time it is used.</code></p>
  </blockquote>
  
  <p>So I wanted to migrate this code. So it was before migration:</p>
  
  <pre><code>class var sharedInstance: CarsConfigurator
  {
      struct Static {
          static var instance: CarsConfigurator?
          static var token: dispatch_once_t = 0
      }
  
      dispatch_once(&amp;Static.token) {
          Static.instance = CarsConfigurator()
      }
  
      return Static.instance!
  }
  </code></pre>
  
  <p>After the migration, following the Apple's guidelines (manual migration), the code looks like this:</p>
  
  <pre><code>class var sharedInstance: CarsConfigurator
  {
      struct Static {
          static var instance: CarsConfigurator?
          static var token = {0}()
      }
  
      _ = Static.token
  
      return Static.instance!
  }
  </code></pre>
  
  <p>But when I run this I get the following error when accessing <code>return Static.instance!</code>:</p>
  
  <blockquote>
    <p>fatal error: unexpectedly found nil while unwrapping an Optional value</p>
  </blockquote>
  
  <p>I see from this error that the <code>instance</code> member is <code>nil</code>, but why is it? Is it something wrong with my migration?</p>