if语句返回的值不适用于UiLabel

时间:2016-03-16 20:54:05

标签: swift if-statement

我对swift和编程很新,我试图从if语句中显示一个值。这是我在UiLabel中的功能。我尝试了相同的几个变化,但它只返回每一个" calculate.fuelTank"它似乎永远不会触发我的IF声明的第二部分?

@IBOutlet weak var startingFuelDisplay: UILabel!  //not working yet
    func refreshUiopeningFuel() {

        if calculate.totalFuel <= Double(calculate.fuelTank) {

            print (Double(calculate.fuelTank)) // FuelTank

        } else {

            print (calculate.totalFuel) // TotalFuel
        }

我是否需要添加bool参数来触发&#34;否则&#34;?我也试过使用&#34; return&#34;函数与初始化字符串,其中包括我试图提取的值,最后我需要这个用于另一个显示。

这是我的全景控制器代码(我是新的,我确定它可以更清洁)

import UIKit

class ViewController: UIViewController {
    let calculate = Inputs ( raceLaps: 13, fuelRate: 3.7, fuelTank: 110, laptime: 85.456, tyreWear: 0.05 )

    @IBOutlet weak var rate: UITextField!
    @IBOutlet weak var laps: UITextField!
    @IBOutlet weak var tank: UITextField!
    @IBOutlet weak var tyreWear: UITextField!
    @IBOutlet weak var laptime: UITextField!

    @IBOutlet var cancelKeyboard: UITapGestureRecognizer!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //Looks for single or multiple taps.
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
        view.addGestureRecognizer(tap)
    }

    //Calls this function when the tap is recognized.
    func dismissKeyboard() {
        //Causes the view (or one of its embedded text fields) to resign the first responder status.
        view.endEditing(true)

            }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
        refreshUiFuel()
        refreshUiStops()
        refreshUiTyreLife()
        refreshUiTyreLifeLaps()
        refreshUiopeningFuel()
        refreshUiBoxOnLap ()


    }



    @IBAction func calculate(sender: UIButton) {
        if let rateVal = Double(rate.text!),
            tankVal = Int(tank.text!),
            lapVal = Int(laps.text!),
            wearVal = Double(tyreWear.text!),
        laptimeVal: Float = 85.456 {
                let fuelModel = Inputs(raceLaps: lapVal, fuelRate: rateVal, fuelTank: tankVal, laptime: laptimeVal, tyreWear: wearVal )
                totalFuelDisplay.text = ("\(Double(fuelModel.totalFuel))")
                totalStopsDisplay.text = ("\(Int(fuelModel.totalStops))")
                tyreLifeDisplay.text = ("\(Int(fuelModel.tyreChangesRaceDistanceTotal))")
                tyreLifeLapsDisplay.text = ("\(Int(fuelModel.tyreLife))")
                startingFuelDisplay.text = ("\(refreshUiopeningFuel())")
        }
        else {
            totalFuelDisplay.text = "missing value"
            totalStopsDisplay.text = "missing value"
            tyreLifeDisplay.text = "missing value"
            tyreLifeLapsDisplay.text = "missing value"
            startingFuelDisplay.text = "missing Value"
        }

                   }

    @IBOutlet weak var totalFuelDisplay: UILabel!
    func refreshUiFuel()->String {
        return totalFuelDisplay.text!
    }

    @IBOutlet weak var totalStopsDisplay: UILabel!
    func refreshUiStops()->String {
        return totalStopsDisplay.text!
    }

    @IBOutlet weak var tyreLifeDisplay: UILabel!
    func refreshUiTyreLife()->String {
        return tyreLifeDisplay.text!
    }
    @IBOutlet weak var tyreLifeLapsDisplay: UILabel!
    func refreshUiTyreLifeLaps()->String {
        return tyreLifeLapsDisplay.text!
    }
    @IBOutlet weak var pitOnLapDisplay: UILabel!
    func refreshUiBoxOnLap () {

    }



    @IBOutlet weak var startingFuelDisplay: UILabel!  //not working yet
        func refreshUiopeningFuel() ->Double {

            print(calculate.fuelTank)
            print(calculate.totalFuel)

            if calculate.totalFuel <= Double(calculate.fuelTank) {

                return Double(calculate.fuelTank) // FuelTank

            } else {

                return calculate.totalFuel // TotalFuel
            }


  }
} 

所有人都欢迎

1 个答案:

答案 0 :(得分:0)

从自然的现实世界逻辑来看,似乎你只是颠倒了if / else逻辑。似乎如果totalFuel小于坦克的容量(if子句),那么在这种情况下你应该返回totalFuel,并在else中返回坦克容量子句。