title: How to properly implement lazy instantiation with an @IBOutlet property in Swift
body: <p>I am Learning iOS Development with Big Nerd Ranch's latest iOS book. I have chosen to implement their apps in Swift. In one of their apps, they have the following code in Objective C:</p>
  
  <pre><code>- (UIView *)headerView
  {
      // If you have not loaded the header view yet...
      if (!_headerView) {
  
          // Load HeaderView.xib
          [[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil]
      }
  
      return _headerView;
  }
  </code></pre>
  
  <p>Apple's Swift guide on "@IBOutlet":</p>
  
  <blockquote>
    <p>When you declare an outlet in Swift, the compiler automatically converts the type to a weak implicitly unwrapped optional and assigns it an initial value of nil. In effect, the compiler replaces @IBOutlet var name: Type with @IBOutlet weak var name: Type! = nil.</p>
  </blockquote>
  
  <p>As it was pointed out in <a href="https://stackoverflow.com/questions/24068829/lazy-loading-properties-in-swift">Lazy loading Properties in swift</a>, there are a couple of different options. None of them in that post explicitly mention lazy initialization with @IBOutlet, so I've done by best to implement their suggestions, and would like to know what would be considered best practices.</p>
  
  <p>Attempt #1(failed): following a similar pattern, as the example from AppDelegate.swift. This brings the issue "'IBOutlet' attribute requires property to be mutable"</p>
  
  <pre><code>@IBOutlet var headerView : UIView  {
      // If the HeaderView has not been loaded yet...
      if !_headerView {
          // Load HeaderView.xib
          NSBundle.mainBundle().loadNibNamed("HeaderView", owner: self, options: nil)
      }
          return _headerView!
  }
  var _headerView : UIView? = nil
  </code></pre>
  
  <p>Attempt #2(failed): using any variation of "@lazy" with "@IBOutlet" didn't worked because "@lazy" needs an initializer, but if a closure is used, then "@IBOutlet" has the same issue as from Attempt #1</p>
  
  <p>Attempt #3(successful?): this is the only way I was able to get this to work. I got the idea from a somewhat different question, <a href="https://stackoverflow.com/questions/24107938/lazy-property-initialization-in-swift">Lazy property initialization in Swift</a>. My understanding of what is happening is headerView is actually declared as "@IBOutlet weak var headerView : UIView! = nil", will only be initialized once with the TableViewController subclass I have, and that initialization will be "lazy" in that it only occurs when the TableViewController needs to be loaded.</p>
  
  <pre><code>@IBOutlet var headerView : UIView
  
  func loadHeaderView() {
      // If the HeaderView has not been loaded yet...
      if !headerView {
          // Load HeaderView.xib
          println("loaded HeaderView")
          NSBundle.mainBundle().loadNibNamed("HeaderView", owner: self, options: nil)
      }
  }
  
  override func viewDidLoad() {
      super.viewDidLoad()
      loadHeaderView()
      tableView.tableHeaderView = headerView
  }
  </code></pre>
  
  <p>So, how can this be improved? </p>
  
  <p>Is viewDidLoad() the correct function to use?</p>
  
  <p>Thanks</p>