gpt4 book ai didi

按下@IBAction 按钮后Swift 保持变量

转载 作者:搜寻专家 更新时间:2023-11-01 05:41:02 24 4
gpt4 key购买 nike

我正在研究“小费计算器”,遇到了问题。一旦我将信息放入 UITextField 并运行应用程序,它就会正确计算。然后我将小费 slider 值或人数调整为另一个值,再次点击计算,tabBill 中的变量与所有其他值一起被清除。如果我注释掉货币格式化程序,它将执行我想要的操作。我怎样才能让变量保持它的值,这样我就可以对一个字段进行调整,然后在保持货币格式的同时再次运行它?请非常具体,因为我对很多此类术语都很陌生。

@IBAction func calculateButtonPress(sender: AnyObject) {
println("refresh requested")

let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle

let tabBill = (billAmountTextField.text as NSString).doubleValue
billAmountTextField.text = numberFormatter.stringFromNumber(tabBill)
billAmountTextField.text != "\(tabBill)"

peoplePaying = (numberOfPeoplePaying.text as NSString).doubleValue

tipTotal = tabBill * tipPercent

tipPerPerson = Double(tipTotal) / Double(peoplePaying)
tipPerPersonAmount.text = "\(tipPerPerson)"
tipPerPersonAmount.text = numberFormatter.stringFromNumber(tipPerPerson)

totalPerPersonDue = (tabBill + tipTotal) / peoplePaying
totalPerPerson.text = "\(totalPerPersonDue)"
totalPerPerson.text = numberFormatter.stringFromNumber(totalPerPersonDue)

tipTotalAmount.text = numberFormatter.stringFromNumber(tipTotal)
totalBillWithTip = Double(tabBill)+Double(tipTotal)

totalBillAmountWithTip.text = numberFormatter.stringFromNumber(totalBillWithTip)

//console output
println("tabBill:\(tabBill)", "sliderValue:\(sliderValue)", "tipPercent:\(tipPercent)", "tipTotal:\(tipTotal)", "tipPerPerson:\(tipPerPerson)", "peoplePaying:\(peoplePaying)", "totalPerPersonDue:\(totalPerPersonDue)", "totalBillWithTip:\(totalBillWithTip)")
}

最佳答案

第一次运行该应用程序时,文本字段中有一个 double 值(假设为 10.00),因此转换工作正常,但随后您将此 double 值转换为货币并保存到文本字段中(现在它的值为 $10.00) ,这个新的文本字段不能像你第一次做的那样转换成 double ,尝试通过这个函数运行文本字段的值:

func currencyStringAsDouble(currencyString: String) -> Double {
let currencyFormatter = NSNumberFormatter()
currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
let cleanedString = currencyString.stringByReplacingOccurrencesOfString(currencyFormatter.currencySymbol, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.groupingSeparator, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.decimalSeparator, withString: "")
let currencyDouble = (text as NSString).doubleValue
return currencyDouble;
}

此函数会再次将您的货币字符串转换回双倍数,在您的情况下您会这样做:

let tabBill = currencyStringAsDouble(billAmountTextField.text)

因此您的最终代码将如下所示:

@IBAction func calculateButtonPress(sender: AnyObject) { println("刷新请求")

    let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle

//***Only line that change***//
let tabBill = currencyStringAsDouble(billAmountTextField.text)

billAmountTextField.text = numberFormatter.stringFromNumber(tabBill)
billAmountTextField.text != "\(tabBill)"

peoplePaying = (numberOfPeoplePaying.text as NSString).doubleValue

tipTotal = tabBill * tipPercent

tipPerPerson = Double(tipTotal) / Double(peoplePaying)
tipPerPersonAmount.text = "\(tipPerPerson)"
tipPerPersonAmount.text = numberFormatter.stringFromNumber(tipPerPerson)

totalPerPersonDue = (tabBill + tipTotal) / peoplePaying
totalPerPerson.text = "\(totalPerPersonDue)"
totalPerPerson.text = numberFormatter.stringFromNumber(totalPerPersonDue)

tipTotalAmount.text = numberFormatter.stringFromNumber(tipTotal)
totalBillWithTip = Double(tabBill)+Double(tipTotal)

totalBillAmountWithTip.text = numberFormatter.stringFromNumber(totalBillWithTip)

//console output
println("tabBill:\(tabBill)", "sliderValue:\(sliderValue)", "tipPercent:\(tipPercent)", "tipTotal:\(tipTotal)", "tipPerPerson:\(tipPerPerson)", "peoplePaying:\(peoplePaying)", "totalPerPersonDue:\(totalPerPersonDue)", "totalBillWithTip:\(totalBillWithTip)")
}

//Add new function to convert currency to double
func currencyStringAsDouble(currencyString: String) -> Double {
let cleanedString = currencyString.stringByReplacingOccurrencesOfString(currencyFormatter.currencySymbol, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.groupingSeparator, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.decimalSeparator, withString: "")
let currencyDouble = (text as NSString).doubleValue
return currencyDouble;
}

关于按下@IBAction 按钮后Swift 保持变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30383492/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com