title: Swift Lazy Properties in More Detail
body: <p>The basic concept of lazy properties is as follows.  </p>
  
  <pre><code>//  I create a function that performs a complex task.
  func getDailyBonus() -&gt; Int
  {
      println("Performing a complex task and making a connection online")
      return random()  
  }
  
  //I set define a property to be lazy and only instantiate it when it is called.  I set the property equal to the function with the complex task 
  class  Employee
  {
      // Properties
      lazy var bonus = getDailyBonus()
  }
  </code></pre>
  
  <p>I started working on a new project with CoreData and noticed that the Core Data stack is set up with Lazy Properties.  However, the code is not something I've seen before and was hoping someone could help me understand the syntax.</p>
  
  <pre><code>// From Apple
  lazy var managedObjectModel: NSManagedObjectModel =
  {
      // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
      let modelURL = NSBundle.mainBundle().URLForResource("FLO_Cycling_1_1_1", withExtension: "momd")!
      return NSManagedObjectModel(contentsOfURL: modelURL)!
  }()
  </code></pre>
  
  <p>Apple uses the { custom code }() syntax. My first thought was that they were simply using a closure when defining the lazy property to eliminate the need to create function first.  Then I tried the following.</p>
  
  <pre><code>//  I tried to define a lazy property that was not an object like so. 
  lazy var check = {
      println("This is your check")
  }()
  </code></pre>
  
  <p>The compiler complained and suggested the following fix.</p>
  
  <pre><code>//  I do not understand the need for the ": ()" after check 
  lazy var check: () = {
      println("This is your check")
  }()
  </code></pre>
  
  <p>Can someone help me understand the syntax?  The () at the end of the CoreData stack and the : () at the end of the check property is confusing me.</p>
  
  <p>Take care,</p>
  
  <p>Jon</p>