I am trying to build an orders array in Swift. Sometimes the values that get filled in them are nil
. This leads to an app crash. Ho do I handle this?
In the code below, I loop through orders and add them to the orders
array:
var orders: [order] = []
//loop
orders[i] = order(dispatchNumber: "\
(myOrder.DispatchNumber!)",orderId: "\
(myOrder.OrderId!)", source: myOrder.SourceName! , sourceAddress1: myOrder.SourceAddress! ,
sourceAddress2: myOrder.SourceCity! + ", " + myOrder.SourceState! +
" " + myOrder.SourceZip! , destination: myOrder.DestinationName!,
destinationAddress1:myOrder.DestinationAddress!, destinationAddress2:
myOrder.DestinationCity! + ", " + myOrder.DestinationState! + " " +
myOrder.DestinationZip! , loads: "\(myOrder.LoadCount!)",
loadsDelivered: "\(myOrder.LoadsDelivered!)", tons: "\
(myOrder.TonsCount!)", price: "$" + "\(myOrder.PayRate!)", sourceDistance: myOrder.DistanceToSource!, onewayDistance: myOrder.OrderLegDistance!, pickupStart: myOrder.PickupBy!, earliestDelivery: myOrder.DeliverStart!,latestDelivery: myOrder.DeliverBy!,product: myOrder.ProductName! , loadsRemaining: "\(thisLoadsRemaining)", truckType: myOrder.TruckType!, notes:
myOrder.Notes!, isStarted: myOrder.IsStarted, isOnHold: myOrder.IsOnHold, payRateType: "\(myOrder.PayRateType!)",
isStayOn: myOrder.IsStayOn, customerName: myOrder.CustomerName! )
Sometimes myOrder.OrderLegDistance!
gets a nil
value. How do I handle it?