Swift 3: Ошибка Поля Расчета

**** УСОВЕРШЕНСТВОВАННЫЙ ****

Последняя сага меня все еще смущает. Как я и советовал, я секвенировал код в соответствии с математическими расчетами. Я все еще получаю ошибку запуска, как и раньше. См.приложение..

@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)"


}

Я запускаю программу и проверяю входные данные. Первые три являются правильными, то он затем переходит рядом с fourthValue перед запуском расчетов в соответствии с порядком выше. Я прикрепил скриншот ниже.

Здесь

Ошибка выполнения в коде также ниже с правильным результатом TFO012.
Здесь

fourthValue should = значение tfo012 101.2, но в списке его 2.16095795579922694 E-314. Откуда он получил этот номер?

Еще раз извините за все вопросы к вам всем. Вы уже оказали мне огромную помощь и если серьезно оценили!!

@Sam M, это полный код контроллера вида

//
//  ViewController.swift
//  TDP Calculator
//
//  Created by Ryan Harris on 24/09/2016.
//  Copyright © 2016 Harris. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var scrollView: UIScrollView!

@IBOutlet var mainbutton: UISwitch!
@IBOutlet var fwdbutton: UISwitch!
@IBOutlet var aftbutton: UISwitch!

@IBOutlet var maindeckimage: UIImageView!
@IBOutlet var fwdholdimage: UIImageView!
@IBOutlet var aftholdimage: UIImageView!

@IBOutlet var ZFWinput: UITextField!
@IBOutlet var TotalFuelinput: UITextField!
@IBOutlet var TripFuelinput: UITextField!

@IBOutlet var RampOutput1: UILabel!
@IBOutlet var TOWOutput1: UILabel!
@IBOutlet var LandOutput1: UILabel!

@IBOutlet var Calculate: UIButton!

@IBOutlet var ZFWOutput2: UILabel!
@IBOutlet var TotalFuelOutput2: UILabel!
@IBOutlet var RampOutput2: UILabel!
@IBOutlet var TOWOutput2: UILabel!
@IBOutlet var TripFuelOutput2: UILabel!
@IBOutlet var LandOutput2: UILabel!

//Switches linked to all cargo bays on profile

@IBAction func maindeckswitch(_ sender: AnyObject) {
    if mainbutton.isOn {
        maindeckimage.isHidden = true
        mainbutton.setOn(false, animated: true)
    }else{
        maindeckimage.isHidden = false
        mainbutton.setOn(true, animated: true)
    }

}


@IBAction func fwdholdswitch(_ sender: AnyObject) {
    if fwdbutton.isOn {
        fwdholdimage.isHidden = true
        fwdbutton.setOn(false, animated: true)
    }else{
        fwdholdimage.isHidden = false
        fwdbutton.setOn(true, animated: true)
}

}


@IBAction func aftholdswitch(_ sender: AnyObject) {
    if aftbutton.isOn {
        aftholdimage.isHidden = true
        aftbutton.setOn(false, animated: true)
    }else{
        aftholdimage.isHidden = false
        aftbutton.setOn(true, animated: true)
    }


}


override func viewDidLoad() {
    super.viewDidLoad()

    //Remove Keyboard with background Touch - Ryan Harris

    let tapRecognizer = UITapGestureRecognizer()
    tapRecognizer.addTarget(self, action: #selector(ViewController.didTapView))
    self.view.addGestureRecognizer(tapRecognizer)

}

//Link function to Keyboard removal

func didTapView(){
    self.view.endEditing(true)
}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.

}



// Calculation Code Starts here

@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)"


}




}

2 ответа

  1. Проверьте значения, возвращаемые текстовыми полями. Убедитесь, что они не равны нулю и правильно конвертируются в числа в соответствующем диапазоне для двойных поплавков, начиная с первого символа (без переднего пробела и т.д.). В противном случае преобразование Double() может вызвать ошибку.

  2. Вы объявляете и используете fourthValueи fifthValueв ваших вычислениях перед установкой TripFuelOutput2.textи TotalFuelOutput2.textзначениями. Вы должны переместить объявление и расчеты с участием fourthValueи fifthValueпосле того, как все if-elses так TripFuelOutput2.textи TotalFuelOutput2.textзначения были установлены.

    Измените код следующим образом:

    @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)"
    
    }