When using a DateFormatter
in the initialization of a struct like below, what's the cleanest way of caching that formatter?
struct Entry {
let date: Date
// ...
init?(string: String) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
if let date = dateFormatter.date(from: string) {
self.date = date
} else {
return nil
}
// ...
}
}