It's well known that creating NSDateFormatters is 'expensive'
Even Apple's Data Formatting Guide (updated 2014-02) states:
Creating a date formatter is not a cheap operation. If you are likely to use a formatter frequently, it is typically more efficient to cache a single instance than to create and dispose of multiple instances. One approach is to use a static variable.
But that doc seems not really up to date with swift and I also can't find anything about that in the latest NSDateFormatter Class Reference about caching the formatter so I can only assume that it's just as expensive for swift as it is for objective-c.
A lot of sources suggest caching the formatter inside the class using it, for example a controller or a view.
I was wondering if it would be handy or even 'cheaper' to add a singleton class to the project to store the datepicker so you're assured it's never necessary to create it again. This could be used everywhere in the app. You could also create several shared instances containing multiple datepickers. For example one datepicker for displaying dates and one for a time notation:
class DateformatterManager {
var formatter = NSDateFormatter()
class var dateFormatManager : DateformatterManager {
struct Static {
static let instance : DateformatterManager = DateformatterManager()
}
// date shown as date in some tableviews
Static.instance.formatter.dateFormat = "yyyy-MM-dd"
return Static.instance
}
class var timeFormatManager : DateformatterManager {
struct Static {
static let instance : DateformatterManager = DateformatterManager()
}
// date shown as time in some tableviews
Static.instance.formatter.dateFormat = "HH:mm"
return Static.instance
}
// MARK: - Helpers
func stringFromDate(date: NSDate) -> String {
return self.formatter.stringFromDate(date)
}
func dateFromString(date: String) -> NSDate? {
return self.formatter.dateFromString(date)!
}
}
// Usage would be something like:
DateformatterManager.dateFormatManager.dateFromString("2014-12-05")
Another likewise approach would be creating just one singleton and switching the format depending on the need:
class DateformatterManager {
var formatter = NSDateFormatter()
var dateFormatter : NSDateFormatter{
get {
// date shown as date in some tableviews
formatter.dateFormat = "yyyy-MM-dd"
return formatter
}
}
var timeFormatter : NSDateFormatter{
get {
// date shown as time in some tableviews
formatter.dateFormat = "HH:mm"
return formatter
}
}
class var sharedManager : DateformatterManager {
struct Static {
static let instance : DateformatterManager = DateformatterManager()
}
return Static.instance
}
// MARK: - Helpers
func dateStringFromDate(date: NSDate) -> String {
return self.dateFormatter.stringFromDate(date)
}
func dateFromDateString(date: String) -> NSDate? {
return self.dateFormatter.dateFromString(date)!
}
func timeStringFromDate(date: NSDate) -> String {
return self.timeFormatter.stringFromDate(date)
}
func dateFromTimeString(date: String) -> NSDate? {
return self.timeFormatter.dateFromString(date)!
}
}
// Usage would be something like:
var DateformatterManager.sharedManager.dateFromDateString("2014-12-05")
Would either of those be a good or a horrible idea? And is switching the format also expensive?
Update: As Hot Licks and Lorenzo Rossi point out, switching the formats is probably not such a good idea (Not thread safe and just as expensive as re-creating..).