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
}
}