Last active
June 24, 2016 18:40
-
-
Save funkyboy/625f9c3adcc65edf14cde392407b68cd to your computer and use it in GitHub Desktop.
One way to create a date formatter only once in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CheapDateFormatter { | |
| private static let dateFormatter = NSDateFormatter() | |
| static func formatter() -> NSDateFormatter { | |
| dateFormatter.dateStyle = .MediumStyle // reconfiguring is as expensive as recreating | |
| dateFormatter.timeStyle = .ShortStyle // not good for performance. See comments below. | |
| return dateFormatter | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Setting the dateFormat / dateStyle is expensive, so you're not really saving anything by having a static reference here.
https://twitter.com/Catfish_Man/status/732982650890113024