Swift For In循环不在Archive中维护变量值

时间:2014-09-10 05:29:03

标签: ios swift xcode6

我有一段代码可以通过Xcode直接部署到设备上,但是如果我通过adhoc存档安装应用程序,它就会失败。

这是代码,它充满了日志语句,因为我无法使用调试器,因此我不得不依赖控制台输出来解决发生的事情。

    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 && 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

问题是除非第一个条件匹配(即它将值作为循环的一部分返回),否则变量status总是等于ShipmentStatus.NOT_READY。如果我检查日志,我可以看到在每个循环迭代status也等于ShipmentStatus.NOT_READY但我也可以在控制台中看到它在迭代期间正确设置。因此,似乎将变量视为已在循环内声明并为每次迭代创建新副本。

正如我所提到的,只有将项目捆绑到存档并通过无线方式部署时才会出现这种情况。直接部署到设备上时,代码按预期工作。在iOS 7.1.2和iOS 8上都会发生这种情况。我刚刚使用Xcode 6 GM测试过,问题仍然存在。

有没有人在他们的代码中看到过这样的行为?奇怪的是我有其他代码遵循类似的模式,它似乎返回了预期的值。

=====

为了澄清,这是我在使用Xcode部署时看到的日志输出

[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

当通过存档部署时,结果是

<Warning>: Beginning Loop Shipment Status = 0
<Warning>: 10000221 Stop Status = 70
<Warning>: Shipment Status = 60
<Warning>: Beginning Loop Shipment Status = 0
<Warning>: 10000222 Stop Status = 70
<Warning>: Shipment Status = 60
<Warning>: Beginning Loop Shipment Status = 0
<Warning>: 10000223 Stop Status = 70
<Warning>: Shipment Status = 60
<Warning>: Return Shipment Status = 0

1 个答案:

答案 0 :(得分:0)

好的,问题已解决,但使用了我认为的解决方法,因为我认为由于问题的性质以及当通过Xcode部署应用程序时它不会导致问题这一事实包装生成器。

ShipmentStatus的枚举定义在与此函数相同的Swift文件中定义。所以我将枚举转移到一个单独的文件中,嘿嘿,问题消失了。