In Objective-C, a class typically uses a static reference to an instance that the class will use several times. For instance,
@implementation MyClass
static NSDateFormatter *dateFormatter = nil;
+ (void) initialize {
if (self == [MyClass class]) {
dateFormatter = [[NSDateFormatter alloc] init];
}
}
@end
In Swift, we no longer need to declare and initialize this static object in two different places. We can just do
let dateFormatter = NSDateFormatter()
in the class area, and date formatting is initialized when the class loads.
My question is: when writing in Swift, is there any reason not to use this new template? It would be possible to declare the date formatting in the module area, and then initialize it to initialize; is there a reason to do it in two steps like this?
source
share