0

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 Leo Dabus's Answer Here 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 What does “fatal error: unexpectedly found nil while unwrapping an Optional value” mean? 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!

I am getting the error here in the amountGivenEditingChanged and cashTipsEditingChanged functions.

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
    }
}
Community
  • 1
  • 1
  • 1
    just post the part that contains the line that have the error – Tj3n Nov 24 '16 at 06:04
  • Where are you getting this nil error. – Nirav D Nov 24 '16 at 06:15
  • @Nirav D in the amountGivenEditingChanged and cashTipsEditingChanged functions: let totalTipsCalc :Double = Double(amountGivenField.text!)! - Double(ticketAmountField.text!)! + Double(cashTipsValue)! – Joel Payne Nov 24 '16 at 07:36
  • Are you sure all these double create a value? Try print all of them first – Tj3n Nov 24 '16 at 07:53
  • @Tj3n okay I made a button and linked it to: print(ticketAmountField.text as Any) print(amountGivenField.text as Any) print(cashTipsField?.text as Any) print(totalTips.text as Any) And it returned: Optional("$0.00") Optional("$0.00") Optional("$0.00") Optional("$0.00") – Joel Payne Nov 24 '16 at 08:04
  • You cant convert your text to double with the $ character, that will give you nil `Double(amountGivenField.text!)!` and will create error – Tj3n Nov 24 '16 at 08:17
  • @Tj3n Ahhhh gotcha okay so if do an : .remove(at: String.startIndex) it should work right? – Joel Payne Nov 24 '16 at 08:35
  • yep, just try print that double before use and see if it get value instead of nil then you can use it – Tj3n Nov 24 '16 at 08:36
  • @Tj3n That worked, thank you! – Joel Payne Nov 24 '16 at 08:51

0 Answers0