title: Correct placement of capture list in nested closures in swift
body: <p>Where do I define captured references for nested closures in Swift?</p>
  
  <p>Take this code as an example:</p>
  
  <pre><code>import Foundation
  
  class ExampleDataSource {
      var content: Any?
      func loadContent() {
          ContentLoader.loadContentFromSource() { [weak self] loadedContent in
              // completion handler called on background thread
              dispatch_async(dispatch_get_main_queue()) { [weak self] in
                  self?.content = loadedContent
              }
          }
      }
  }
  
  class ContentLoader {
      class func loadContentFromSource(completion: (loadedContent: Any?) -&gt; Void) {
          /*
          Load content from web asynchronously, 
          and call completion handler on background thread.
          */
      }
  }
  </code></pre>
  
  <p>In this example, <code>[weak self]</code> is used in both trailing closures, however the compiler is perfectly happy if I omit <code>[weak self]</code> from either one of the trailing closures.</p>
  
  <p>So that leaves me 3 options for defining my capture list:</p>
  
  <ol>
  <li>define captures on every nested closure leading up to the reference</li>
  <li>define captures on the first closure only.</li>
  <li>define captures on only the most nested closure that actually uses the reference.</li>
  </ol>
  
  <p>My question is:  </p>
  
  <blockquote>
    <p>If I know that my <code>ExampleDataSource</code> could be <code>nil</code> at some point, what is the best option to go with?</p>
  </blockquote>