Since my first post on formatting a UITextField with proper currency symbols and separators I think I have learned a lot more about objective-c and more importantly the UI components provided by Apple. In this article I will revisit how to format a UITextField with the correct currency symbol and grouping separators.
The original goal was to create a text field that would update in real time both with the currency symbol and grouping separators as numbers were typed in. The first working solution can be found here, but it had some short comings particularly in code complexity. The main problem was that I was trying to keep the string formatting correct so that I could decode the string to an NSNumber using a currency formatter and then encode the string again to an NSString. This was needless complicating the code when dealing with inputting characters. The original code:
Instead it was much easier to just strip out the currency symbol and group separators and then use a basic number formatter to turn the string into a NSNumber. I was able to remove an entire section of code:
Finally, take the newly formed NSNumber and run it through the currency formatter to create the desired output. The entire new method is below:
First thing to notice is the cleanup of the code between lines 19 and 21. There is no more special case, and thus reduced complexity. Line 32 is what allowed the removal of the special case. Instead of trying to work with symbols and groupings they are now ignored completely and removed before being added back in. The variable clean_string will end up a string with only numbers and will be easily parsed into an NSNumber by a basic NSNumberFormatter.
There are a few class instance variables that need to be set. The init and associated dealloc is below even though they are not necessarily related to the optimizations above.
Formatters is a custom class that contains various class methods to return ready to use formatters. In the next article I will show how it is setup. In the meantime two new NSNumberFormatters could have just been declared inline. See the Apple reference documentation for NSNumberFormatter.
The above improvement cut out some code that was not needed, but most of all made the logic easier to follow. Instead of having to worry about special cases with currency symbols when inserting new characters, they can now just be ignored. The formatters do all of the heavy lifting as they should.