使用货币格式化UITextfield进行数学运算

时间:2016-11-24 05:59:30

标签: ios swift3

我一直在为我的工作提供一个交付跟踪器应用程序,同时提供比萨饼,但我希望将文本字段格式化为货币。我从Leo Dabus's Answer Here借用了代码并在我的代码中使用了它,但它不适用于我正在尝试的内容。我一直收到一个错误:“致命错误:在打开一个Optional值时意外地发现了nil”。这与What does “fatal error: unexpectedly found nil while unwrapping an Optional value” mean?不同,因为它不处理自定义CurrencyField类。 我已经搜索并搜索了一个解决方案,但是当我尝试实现它们时它不起作用。它可以工作,如果我使用UITextField类而不是CurrencyField类,但我想将该字段格式化为货币,并启用快速输入与自动小数位。 有任何想法吗?谢谢你的帮助!

我在amountGivenEditingChanged和cashTipsEditingChanged函数中收到错误。

let totalTipsCalc:Double = Double(amountGivenField.text!)! - Double(ticketAmountField.text!)! + Double(cashTipsValue)!

    class EditDeliveryViewController: UIViewController,  {

// MARK: Storyboard Actions

@IBAction func amountGivenEditingChanged(_ sender: Any) {             
    // Calculate
// Error Here
    if (cashTipsField?.text) != nil {
        let cashTipsValue = cashTipsField?.text ?? "0"
        let totalTipsCalc :Double = Double(amountGivenField.text!)! - Double(ticketAmountField.text!)! + Double(cashTipsValue)!
        totalTips.text = "$" + String(format: "%.2f", totalTipsCalc)
    } else {
        let totalTipsCalc :Double = Double(amountGivenField.text!)! - Double(ticketAmountField.text!)!
        totalTips.text = "$" + String(format: "%.2f", totalTipsCalc)
    }
@IBAction func cashTipsEditingChanged(_ sender: Any) {
    // Calculate
// And Error Here
     if (cashTipsField?.text) != nil {
        let totalTipsCalc :Double = Double(amountGivenField.text!)! - Double(ticketAmountField.text!)! + Double(cashTipsField!.text!)!
        totalTips.text = "$" + String(format: "%.2f", totalTipsCalc)
    } else {
        let totalTipsCalc :Double = Double(amountGivenField.text!)! - Double(ticketAmountField.text!)!
        totalTips.text = "$" + String(format: "%.2f", totalTipsCalc)
    }

// MARK: Storyboard Outlets

@IBOutlet var ticketNumberField: UITextField!
@IBOutlet var ticketAmountField: CurrencyField!
@IBOutlet var amountGivenField: CurrencyField!
@IBOutlet var cashTipsField: CurrencyField?
@IBOutlet var totalTips: UILabel!

// MARK: CurrencyField Class
  class CurrencyField: UITextField {
      override func awakeFromNib() {
        super.awakeFromNib()
        addTarget(self, action: #selector(editingChanged), for: .editingChanged)
        keyboardType = .numberPad
        textAlignment = .right
        editingChanged()
    }
    func editingChanged() {
        text = Formatter.currency.string(from: (Double(string.numbers.integer) / 100) as NSNumber)
    }
}

struct Formatter {
    static let currency = NumberFormatter(numberStyle: .currency)
}

extension UITextField {
    var string: String { return text ?? "0" }
}

extension String {
    var numbers: String { return components(separatedBy: Numbers.characterSet.inverted).joined() }
    var integer: Int { return Int(numbers) ?? 0 }
}

struct Numbers { static let characterSet = CharacterSet(charactersIn: "0123456789") }

extension NumberFormatter {
    convenience init(numberStyle: NumberFormatter.Style) {
        self.init()
        self.numberStyle = numberStyle
    }
}

0 个答案:

没有答案