0

I am subclassing UITextField to create a currency text field. The text field auto formats number being entered into currency as it is being typed. I've got it working with one issue. For whatever reason the last number typed gets doubled. Say for example I want enter $56.78, so I key in 5678, however the output ends up being $56.788.

I've printed every possible scenario to the console and the correct version prints there (in this case 56.78). I've narrowed it down I believe to the formatCurrency function but I cannot figure out for the life of me what is going on. Here is the relevant code:

class CurrencyTextField: UITextField, UITextFieldDelegate {

    private var currencyString = String()

    func textFieldDidBeginEditing(_ textField: UITextField) {

        // Clear out the current string when the user starts editing the field
        //currencyString = ""
    }

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

        switch string {
        case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
            currencyString += string
            self.text = formatCurrency(currencyString)
            return true
        default:
            return false
        }

    }

    func textFieldShouldClear(_ textField: UITextField) -> Bool {

        // Clear the current string if the user clicks the clear button
         currencyString = ""
        return true
    }

    private func formatCurrency(_ string: String) -> String {
        let numberToFormat = Double(string)! / 100

        let formatter = NumberFormatter()
        formatter.numberStyle = .currency

        if let stringToReturn = formatter.string(from: NSNumber(value: numberToFormat)) {
            return stringToReturn
        } else {
            return "$0.00"
        }
    }
}

Any insight would be highly appreciated! I used very similar code without subclassing and didn't have any issues. Additionally, if I bypass the formatting and just use a straight number it works as well.

BONUS if you have any thoughts on why given this same code that the backspace key on the number pad doesn't work.

EDIT: This is not a duplicate as marked as the case it was said to duplicate shows how to do what I am already doing, it doesn't even mention the problem I am having.

  • The case you refer to doesn't even mention the problem I mentioned with the duplicating trailing digit. – Tim Bryant Sep 23 '16 at 01:40
  • Yes that code accomplishes what I'm trying to do but it does not help me or anyone else with the same problem I was having learn what is wrong. I appreciate you pointing to a completely different solution, however it doesn't solve the problem with this solution. I would appreciate if you would please reopen this case so myself and others can learn how to solve this specific problem. – Tim Bryant Sep 23 '16 at 02:11
  • Additionally, correct me if I'm wrong, but the proposed solution doesn't exactly follow the delegation pattern. I could be wrong but it's not using any of the delegate methods of UITextField. – Tim Bryant Sep 23 '16 at 02:14
  • @LeoDabus where did all your comments go? – Tim Bryant Sep 23 '16 at 02:22
  • Could you give some more detail about what you mean by "I printed every scenario to the console"? It would be helpful to know what code you used to do this. – Benjamin Lowry Sep 23 '16 at 02:29
  • Sure, I essentially added print(currencyString) and print(self.text) after every line in the main function to see if it ever printed that second character and it always appeared to print it the way I desired it to be printed. – Tim Bryant Sep 23 '16 at 02:32
  • So even when your used `print(self.text)` after the line `self.text = formatCurrency(currencyString)` it printed 56.78 but showed 56.788 in the textfield? Just trying to make sure I understand. – Benjamin Lowry Sep 23 '16 at 02:36
  • I did just test it again and if I do print(self.text) both before and after that line it shows correctly for the first digit, but if I enter a second digit, then the one before the line self.text = formatCurrency(currencyString) shows the extra digit. The one after is however correct. – Tim Bryant Sep 23 '16 at 02:53
  • Perhaps changing the text by self.text is not the safest method. If the variable is changed but for some reason the text field doesn't update then you would have your problem. Perhaps investigate other methods. – Benjamin Lowry Sep 23 '16 at 02:57

0 Answers0