I am new to swift and am very unfamiliar with Objective-C. Could someone help me convert this to Swift? I got this code from Ray Wenderlich's best iOS practices - http://www.raywenderlich.com/31166/25-ios-app-performance-tips-tricks
Where would you put this code? Would it go in a class file full of global variables?
- (NSDateFormatter *)formatter {
static NSDateFormatter *formatter;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_formatter = [[NSDateFormatter alloc] init];
_formatter.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy"; // twitter date format
});
return formatter;
}
As Rob said, you must be careful with performance creating DateFormatters, use the tips from that WWDC session.
I am doing this as extensions of NSDate and NSDateFormatter (code tested in Swift 2, 3 and 4):
Usage:
You could do it like that and move the
NSDateFormatter
and theonceToken
outside your function. Then you can do it like in your example:But as David suggested, you should take a look at the dispatch_once singleton question.
If you're really looking for direct analog to the method in your question, in Swift 3 you could do something like:
Or in Swift 2:
The
static
properties, like globals, enjoydispatch_once
behavior for their default values. For more information, see thedispatch_once
discussion at the end of the Files and Initialization entry in Apple's Swift blog.Regarding best practices with date formatters, I would suggest:
Yes, it is prudent to not unnecessarily create and destroy formatters that you're likely to need again. In WWDC 2012 video, iOS App Performance: Responsiveness, Apple explicitly encourages us to
Cache one formatter per date format;
Add observer for
NSLocale.currentLocaleDidChangeNotification
in Swift 3 (NSCurrentLocaleDidChangeNotification
in Swift 2) through theNSNotificationCenter
, and clearing/resetting cached formatters if this occurs; andNote that resetting a format is as expensive as recreating, so avoid repeatedly changing a formatter's format string.
Bottom line, reuse date formatters wherever possible if you're using the same date format repeatedly.
If using
NSDateFormatter
to parse date strings to be exchanged with a web service (or stored in a database), you should useen_US_POSIX
locale to avoid problems with international users who might not be using Gregorian calendars. See Apple Technical Q&A #1480. (Or in iOS 10 and macOS 10.12, useISO8601DateFormatter
.)But when using
dateFormat
withNSDateFormatter
/DateFormatter
, use the current locale for creating strings to be presented to the end user, but useen_US_POSIX
local when creating/parsing strings to be used internally within the app or to be exchanged with a web service.If formatting date strings for the user interface, localize the strings by avoiding using string literals for
dateFormat
if possible. UsedateStyle
andtimeStyle
where you can. And if you must use customdateFormat
, use templates to localize your strings. For example, rather than hard-codingE, MMM d
, in Swift 3, you would usedateFormat(fromTemplate:options:locale:)
:Or in Swift 2,
dateFormatFromTemplate(_:options:locale:)
:This will automatically show, for example, "Mon, Sep 5" for US users, but "Mon 5 Sep" for UK users.
The
NSDateFormatter
is now thread safe on iOS 7 and later and 64-bit apps running on macOS 10.9 and later.I've tried to apply the Singleton approach found here. This creates a singleton that could be loaded up with different the different formats you use within your application. For example, you could access it anywhere in your application with
formatter.fromDateTime(myString)