title: Swift For In loop not maintaining variable value in Archive
body: <p>I have a piece of code that works fine when deployed via Xcode directly onto the device but if I install the application via an adhoc archive it fails miserably.</p>
  
  <p>This is the code, it is full of log statements as I wasn't able to utilise the debugger so I had to rely on the console output to work out what was happening.</p>
  
  <pre><code>    var status: ShipmentStatus = ShipmentStatus.NOT_READY
      let stopOrders = getStopOrders(true)
  
      for order in stopOrders{
          NSLog("Beginning Loop Shipment Status = %d", status.toRaw())
          let stopStatus = order.getStopOrderStatus()
          NSLog("%d Stop Status = %d", order.salesOrderHeader.salesOrderNo, stopStatus.toRaw())
          if(stopStatus == StopOrderStatus.READY_FOR_PICKING || stopStatus == StopOrderStatus.PARTIALLY_PICKED){
              return ShipmentStatus.READY_FOR_PICKING
          } else if(stopStatus == StopOrderStatus.PICKING_COMPLETE){
              status = ShipmentStatus.PICKING_COMPLETE
              NSLog("Shipment Status = %d", status.toRaw())
          } else if(stopStatus == StopOrderStatus.READY_FOR_DELIVERY || stopStatus == StopOrderStatus.PARTIALLY_DELIVERED){
              if(status != ShipmentStatus.PICKING_COMPLETE){
                  status = ShipmentStatus.READY_FOR_DELIVERY
                  NSLog("Shipment Status = %d", status.toRaw())
              } else {
                  NSLog("Shipment Status is not changed")
              }
          } else if(stopStatus == StopOrderStatus.DELIVERY_COMPLETE){
              if(status != ShipmentStatus.PICKING_COMPLETE &amp;&amp; status != ShipmentStatus.READY_FOR_DELIVERY){
                  status = ShipmentStatus.DELIVERY_COMPLETE
                  NSLog("Shipment Status = %d", status.toRaw())
              } else {
                  NSLog("Shipment Status is not changed")
              }
          }
      }
      NSLog("Return Shipment Status = %d", status.toRaw())
      return status
  </code></pre>
  
  <p>The issue is that unless the first condition is matched (ie it returns the value as part of the loop) the variable <code>status</code> is always equal to <code>ShipmentStatus.NOT_READY</code>. If I check the logs I can see that at every loop iteration <code>status</code> is also equal to <code>ShipmentStatus.NOT_READY</code> but I can also see in the console that it is being set correctly during the iteration. So it seems to be treating the variable as if it has been declared inside the loop and creating a new copy for every iteration.</p>
  
  <p>As I mentioned this only occurs if the project is bundled up into an archive and deployed over the air. When deployed directly onto the device the code works as expected. This happens on both iOS 7.1.2 and iOS 8. I have just tested using Xcode 6 GM and the issue still exists.</p>
  
  <p>Has anyone seen behaviour like this in their code? The strange thing is I have other code which follows a similar pattern and it appears to be returning the expected value.</p>
  
  <p>================</p>
  
  <p>Just for clarification, this is the log output that I see when deployed using Xcode</p>
  
  <pre><code>[7454:60b] Beginning Loop Shipment Status = 0
  [7454:60b] 10000221 Stop Status = 70
  [7454:60b] Shipment Status = 60
  [7454:60b] Beginning Loop Shipment Status = 60
  [7454:60b] 10000222 Stop Status = 70
  [7454:60b] Shipment Status = 60
  [7454:60b] Beginning Loop Shipment Status = 60
  [7454:60b] 10000223 Stop Status = 70
  [7454:60b] Shipment Status = 60
  [7454:60b] Return Shipment Status = 60
  </code></pre>
  
  <p>And when it is deployed via an archive the result is</p>
  
  <pre><code>&lt;Warning&gt;: Beginning Loop Shipment Status = 0
  &lt;Warning&gt;: 10000221 Stop Status = 70
  &lt;Warning&gt;: Shipment Status = 60
  &lt;Warning&gt;: Beginning Loop Shipment Status = 0
  &lt;Warning&gt;: 10000222 Stop Status = 70
  &lt;Warning&gt;: Shipment Status = 60
  &lt;Warning&gt;: Beginning Loop Shipment Status = 0
  &lt;Warning&gt;: 10000223 Stop Status = 70
  &lt;Warning&gt;: Shipment Status = 60
  &lt;Warning&gt;: Return Shipment Status = 0
  </code></pre>