找到你要的答案

Q:How do I use my entered UITextField Value as parameter value for swift function?

Q:我如何使用我的输入值作为函数参数的UITextField敏捷的价值?

I need to be able to take my UITextField value and use it as a Double to perform the computation that is in my function; however it isn't returning the correct value. I want to take total dollar amount and multiply it by a percent discount. Here is my code:

//Text Fields
    @IBOutlet weak var amountTextField: UITextField!
    @IBOutlet weak var percentDiscountTextField: UITextField!

    func calculateAmount(percentage: Double) -> Double{

        var percentage = (percentDiscountTextField.text as NSString).doubleValue
        return (amountTextField.text as NSString).doubleValue - ((amountTextField.text as NSString).doubleValue *  percentage)/100
    }

    @IBAction func calculatePressed(sender: AnyObject) {

        var alert = UIAlertController(title: "Alert", message: "Please Enter a Value", preferredStyle: UIAlertControllerStyle.Alert)

        if (count(amountTextField.text) == 0){

            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alert, animated: true, completion: nil)

        } else if (count(percentDiscountTextField.text) == 0){

            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alert, animated: true, completion: nil)

        } else {

            var finalPrice: Double = calculateAmount()

        }

    }

我需要把我的UITextField价值和使用它作为一个双执行计算,是在我的功能;但是它不是返回正确的值。我想取总金额,乘以九九折。这是我的代码:

//Text Fields
    @IBOutlet weak var amountTextField: UITextField!
    @IBOutlet weak var percentDiscountTextField: UITextField!

    func calculateAmount(percentage: Double) -> Double{

        var percentage = (percentDiscountTextField.text as NSString).doubleValue
        return (amountTextField.text as NSString).doubleValue - ((amountTextField.text as NSString).doubleValue *  percentage)/100
    }

    @IBAction func calculatePressed(sender: AnyObject) {

        var alert = UIAlertController(title: "Alert", message: "Please Enter a Value", preferredStyle: UIAlertControllerStyle.Alert)

        if (count(amountTextField.text) == 0){

            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alert, animated: true, completion: nil)

        } else if (count(percentDiscountTextField.text) == 0){

            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alert, animated: true, completion: nil)

        } else {

            var finalPrice: Double = calculateAmount()

        }

    }
answer1: 回答1:

I'm a bit confused as to why the method calculateAmount takes an argument, but when you're calling it you're not suppling an argument?

I'll assume calculateAmount isn't supposed to take any arguments, here's how I'd write the method:

func calculateAmount() -> Double? {
    let percentageFromTextField = (percentDiscountTextField.text as NSString).doubleValue
    let percentageOff = percentageFromTextField / 100.0

    //  Best to check the user entered a value in the correct range! 
    //  Perhaps you could display a message saying the percentage discount
    //  wasn't in the correct range (if nil is returned)
    if percentageOff < 0 || percentageOff > 1 { return nil }

    let amount = (amountTextField.text as NSString).doubleValue
    return amount * (1 - percentageOff)
}

Hope that helps!

我有点困惑的是为什么calculateamount的方法需要一个参数,但当你调用它你不提供一个说法?

我会认为calculateamount不应该采取任何争论,这就是我写的方法:

func calculateAmount() -> Double? {
    let percentageFromTextField = (percentDiscountTextField.text as NSString).doubleValue
    let percentageOff = percentageFromTextField / 100.0

    //  Best to check the user entered a value in the correct range! 
    //  Perhaps you could display a message saying the percentage discount
    //  wasn't in the correct range (if nil is returned)
    if percentageOff < 0 || percentageOff > 1 { return nil }

    let amount = (amountTextField.text as NSString).doubleValue
    return amount * (1 - percentageOff)
}

希望帮助!

swift