title: Using a currency formatted UITextfield to do math operations with
body: <p>I have been working a Delivery tracker app for my job while delivering pizzas but I would like the textfields to be formatted as a currency. I borrowed code from <a href="https://stackoverflow.com/a/29783546/7203490">Leo Dabus's Answer Here</a> and used it in mine but it won't work with what I'm trying to do. I keep getting an error: "fatal error: unexpectedly found nil while unwrapping an Optional value". This is different from <a href="https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu">What does “fatal error: unexpectedly found nil while unwrapping an Optional value” mean?</a> because it doesn't deal with the custom CurrencyField class.
  I have searched and searched for a solution for this but when I try and implement them it doesn't work. It works if I use the UITextField Class instead of the CurrencyField Class but I would like to format the field as a currency and enable quick entry with auto decimal placement.
  Any ideas? Thanks for any help!</p>
  
  <p>I am getting the error here in the amountGivenEditingChanged and cashTipsEditingChanged functions.</p>
  
  <p>let totalTipsCalc :Double = Double(amountGivenField.text!)! - Double(ticketAmountField.text!)! + Double(cashTipsValue)!</p>
  
  <pre><code>    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
      }
  }
  </code></pre>