RyanHK RyanHK - 2 months ago 5
Swift Question

Swift 3: Calculation Field Error

**** UPDATED ****

The latest saga has me still confused. As advised i sequenced the code as per the mathmatical calculations. I am still however getting a run error as before. Please see attached..

@IBAction func Calculate(_ sender: AnyObject) {

let firstValue = Double(ZFWinput.text!)
let secondValue = Double(TotalFuelinput.text!)
let thirdValue = Double(TripFuelinput.text!)

let TFO000 = Double(secondValue!)
let TFO004 = Double(secondValue! * 1.004)
let TFO006 = Double(secondValue! * 1.006)
let TFO007 = Double(secondValue! * 1.007)
let TFO010 = Double(secondValue! * 1.01)
let TFO011 = Double(secondValue! * 1.011)
let TFO012 = Double(secondValue! * 1.012)

let TripF000 = Double(thirdValue!)
let TripF004 = Double(thirdValue! * 1.004)
let TripF006 = Double(thirdValue! * 1.006)
let TripF007 = Double(thirdValue! * 1.007)
let TripF010 = Double(thirdValue! * 1.10)
let TripF011 = Double(thirdValue! * 1.11)
let TripF012 = Double (thirdValue! * 1.12)


// *Self Reminder* Calculation for Total Fuel Adjustment with various TDP Loaded

if mainbutton.isOn && fwdbutton.isOn && aftbutton.isOn {
TotalFuelOutput2.text = "\(TFO012)"
}
else if fwdbutton.isOn && aftbutton.isOn {
TotalFuelOutput2.text = "\(TFO012)"
}
else if mainbutton.isOn && fwdbutton.isOn {
TotalFuelOutput2.text = "\(TFO011)"
}
else if mainbutton.isOn && aftbutton.isOn {
TotalFuelOutput2.text = "\(TFO010)"
}
else if aftbutton.isOn {
TotalFuelOutput2.text = "\(TFO006)"
}
else if fwdbutton.isOn {
TotalFuelOutput2.text = "\(TFO007)"
}
else if mainbutton.isOn {
TotalFuelOutput2.text = "\(TFO004)"
}
else {TotalFuelOutput2.text = "\(TFO000)"}


// *Self Reminder* Calculation for Trip Fuel Adjust with various TDP Loaded

if mainbutton.isOn && fwdbutton.isOn && aftbutton.isOn {
TripFuelOutput2.text = "\(TripF012)"
}
else if fwdbutton.isOn && aftbutton.isOn {
TripFuelOutput2.text = "\(TripF012)"
}
else if mainbutton.isOn && fwdbutton.isOn {
TripFuelOutput2.text = "\(TripF011)"
}
else if mainbutton.isOn && aftbutton.isOn {
TripFuelOutput2.text = "\(TripF010)"
}
else if aftbutton.isOn {
TripFuelOutput2.text = "\(TripF006)"
}
else if fwdbutton.isOn {
TripFuelOutput2.text = "\(TripF007)"
}
else if mainbutton.isOn {
TripFuelOutput2.text = "\(TripF004)"
}
else {TripFuelOutput2.text = "\(TripF000)"}

//----
let fourthValue = Double(TotalFuelOutput2.text!)
let fifthValue = Double(TripFuelOutput2.text!)

let RampWeight1 = Double(firstValue! + secondValue!)
let TOW1 = Double(RampWeight1 - 1)
let LMass1 = (TOW1 - thirdValue!)

let RampWeight2 = Double(firstValue! + fourthValue!)
let TOW2 = Double(RampWeight2 - 1)
let LMass2 = (TOW2 - fifthValue!)

// *Self Reminder* Calculation for center figuers with no TDP adjustments

RampOutput1.text = "\(RampWeight1)"
TOWOutput1.text = "\(TOW1)"
LandOutput1.text = "\(LMass1)"

// *Self Reminder* Calculation for right hand side figures using new TotalFuelOutput2 & TripFuelOutput2

RampOutput2.text = "\(RampWeight2)"
TOWOutput2.text = "\(TOW2)"
LandOutput2.text = "\(LMass2)"


}


I run the program and check the inputs. The first three are correct then it then jumps next to fourthValue before running the calculations as per the order above. I have attached the screenshot below.

Here

A runtime error in the code is also below with the correct TFO012 result.
Here

fourthValue should = the TFO012 value of 101.2, but in the list its 2.16095795579922694E-314. Where has it received this number from?

Again sorry for all the questions to you all. You have already been a HUGE help to me and if seriously appreciated!!

Answer

You're declaring and using fourthValue and fifthValue in your calculations before setting TripFuelOutput2.text and TotalFuelOutput2.text values. You should move the declaration and calculations involving fourthValue and fifthValue after all the if-elses so TripFuelOutput2.text and TotalFuelOutput2.text values have been set.

Modify your code like this:

@IBAction func Calculate(_ sender: AnyObject) {

    let firstValue = Double(ZFWinput.text!)
    let secondValue = Double(TotalFuelinput.text!)
    let thirdValue = Double(TripFuelinput.text!)

    let TFO000 = Double(secondValue!)
    let TFO004 = Double(secondValue! * 1.004)
    let TFO006 = Double(secondValue! * 1.006)
    let TFO007 = Double(secondValue! * 1.007)
    let TFO010 = Double(secondValue! * 1.01)
    let TFO011 = Double(secondValue! * 1.011)
    let TFO012 = Double(secondValue! * 1.012)

    let TripF000 = Double(thirdValue!)
    let TripF004 = Double(thirdValue! * 1.004)
    let TripF006 = Double(thirdValue! * 1.006)
    let TripF007 = Double(thirdValue! * 1.007)
    let TripF010 = Double(thirdValue! * 1.10)
    let TripF011 = Double(thirdValue! * 1.11)
    let TripF012 = Double (thirdValue! * 1.12)


    // *Self Reminder* Calculation for Total Fuel Adjustment with various TDP Loaded

    if mainbutton.isOn && fwdbutton.isOn && aftbutton.isOn {
        TotalFuelOutput2.text = "\(TFO012)"
    }
    else if fwdbutton.isOn && aftbutton.isOn {
        TotalFuelOutput2.text = "\(TFO012)"
    }
    else if mainbutton.isOn && fwdbutton.isOn {
        TotalFuelOutput2.text = "\(TFO011)"
    }
    else if mainbutton.isOn && aftbutton.isOn {
        TotalFuelOutput2.text = "\(TFO010)"
    }
    else if aftbutton.isOn {
        TotalFuelOutput2.text = "\(TFO006)"
    }
    else if fwdbutton.isOn {
        TotalFuelOutput2.text = "\(TFO007)"
    }
    else if mainbutton.isOn {
        TotalFuelOutput2.text = "\(TFO004)"
    }
    else {TotalFuelOutput2.text = "\(TFO000)"}


    // *Self Reminder* Calculation for Trip Fuel Adjust with various TDP Loaded

    if mainbutton.isOn && fwdbutton.isOn && aftbutton.isOn {
        TripFuelOutput2.text = "\(TripF012)"
    }
    else if fwdbutton.isOn && aftbutton.isOn {
        TripFuelOutput2.text = "\(TripF012)"
    }
    else if mainbutton.isOn && fwdbutton.isOn {
        TripFuelOutput2.text = "\(TripF011)"
    }
    else if mainbutton.isOn && aftbutton.isOn {
        TripFuelOutput2.text = "\(TripF010)"
    }
    else if aftbutton.isOn {
        TripFuelOutput2.text = "\(TripF006)"
    }
    else if fwdbutton.isOn {
        TripFuelOutput2.text = "\(TripF007)"
    }
    else if mainbutton.isOn {
        TripFuelOutput2.text = "\(TripF004)"
    }
    else {TripFuelOutput2.text = "\(TripF000)"}

    //----
    let fourthValue = Double(TotalFuelOutput2.text!)
    let fifthValue = Double(TripFuelOutput2.text!)

    let RampWeight1 = Double(firstValue! + secondValue!)
    let TOW1 = Double(RampWeight1 - 1)
    let LMass1 = (TOW1 - thirdValue!)

    let RampWeight2 = Double(firstValue! + fourthValue!)
    let TOW2 = Double(RampWeight2 - 1)
    let LMass2 = (TOW2 - fifthValue!)

    // *Self Reminder* Calculation for center figuers with no TDP adjustments

    RampOutput1.text = "\(RampWeight1)"
    TOWOutput1.text = "\(TOW1)"
    LandOutput1.text = "\(LMass1)"

    // *Self Reminder* Calculation for right hand side figures using new TotalFuelOutput2 & TripFuelOutput2

    RampOutput2.text = "\(RampWeight2)"
    TOWOutput2.text = "\(TOW2)"
    LandOutput2.text = "\(LMass2)"

}
Comments