title: Block stored var in Swift 2.1
body: <p>So, I'm doing an async query which returns multiple Worker objects, each Worker has a relation with the WorkerPeriod and I use this relation to do a query, the problem is, the query is async, and when it gets back from the web service, the workerIndex is messed up</p>
  
  <pre><code> workerQuery.whereKey("construction", equalTo: currentConstruction).findObjectsInBackgroundWithBlock({ (unsafeWorkers, error) -&gt; Void in
              guard let workers = unsafeWorkers as? [Worker] where error == nil &amp;&amp; workers.count == 0 else {
                  return
              }
  
              for var workerIndex = 0; workerIndex &lt; workers.count; workerIndex++ {
  
                  let currentWorker = workers[workerIndex]
  
  
                  if let periodQuery = WorkerPeriod.query() {
                      periodQuery.whereKey("worker", equalTo: currentWorker)
  
                      let weakWorkerIndex = workerIndex
                      print("weakWorkerIndex = \(weakWorkerIndex) worker \(workerIndex)")
                      periodQuery.findObjectsInBackgroundWithBlock({ (unsafeWorkerPeriod, error) -&gt; Void in
  
                          guard let workerPeriods = unsafeWorkerPeriod as? [WorkerPeriod] where error == nil &amp;&amp; workerPeriods.count != 0 else {
                              return
                          }
                          print("block weakWorkerIndex = \(weakWorkerIndex) workerIndex \(workerIndex)")
  
                          .
                          .
  
                      })
                  }
              }
  </code></pre>
  
  <p>I've tried to create a "weakWorkerIndex" as a let to hold the value as we did in Objective-C with the __block variables, but that doesn't work, the block interaction just ignores the value as you can see in the output:</p>
  
  <blockquote>
  <pre><code>weakWorkerIndex = 0 worker 0
  weakWorkerIndex = 1 worker 1
  block weakWorkerIndex = 0 workerIndex 2
  block weakWorkerIndex = 0 workerIndex 2
  block weakWorkerIndex = 0 workerIndex 2
  </code></pre>
  </blockquote>
  
  <p>By the way: I removed any unnecessary code to simplify the reading.</p>