No matter the language dealing with calendars, dates and dateformatters are cumbersome. Even beginners with Cocoa/CocoaTouch learn very quickly that
NSDateFormatters are expensive to create. There are various techniques that have been used over the years to improve the speed and efficiency of using
- Lazy loading
- Retain the date formatter instance in your class as an attribute
- Using NSThreadDictionary
No matter which one of these techniques I used I was never really satisified with the overall performance or their ease of use. I needed to be able to go back and forth between dates and their respective string representations in a non repeated manner. writing the same code/logic repeatedly
ISO Love NSValueTransformer
Of all the Foundation classes, NSValueTransformer is perhaps the one that fared the worst in the shift from OS X to iOS.
But you know what? NSValueTransformer is ripe for a comeback. With a little bit of re-tooling and some recontextualization, this blast from the past could be the next big thing in your application.
After reading through the TransformerKit and the examples, it dawned on me that I could solve my “date” problems using
NSValueTransformer. Since TransformerKit already provides an interface that removed all the boilerplate setup for the subclasses I forked the project and got to work.
The 3 features that I wanted to provide were:
- The ability to set a default date formatter.
- The ability to cache all custom date formatters
- Provide thread safety
Kiss the Date Goodbye
This is one of the main reasons why I love open source software. Matt not only took the time to point out where my logic was flawed, but offered a better abstract approach and then provided a better technical solution.
I was trying to be all things to all people with what the transformer would handle for you, but in the end transformers should be used to solve a particular issue.
Exposing them as class variables is a good API decision on that count, but introduces shared state to the point of not being particularly useful (e.g. wanting more than one at one time).
Though the pull request was denied, and rightfully so, a new addition was added that provides the functionality that I needed.
Your patch did remind me of one of the reasons I made this library to begin with: converting between ISO 8601 timestamps and NSDates. I’ve added these in c064303.