I’m confused that some instance would be initialized, like this:
NSDate *new = [[NSDate alloc] init];
But some others are not, like this:
NSCalendar *new = [NSCalendar currentCalendar];
What is the difference between them?
I’m confused that some instance would be initialized, like this:
NSDate *new = [[NSDate alloc] init];
But some others are not, like this:
NSCalendar *new = [NSCalendar currentCalendar];
What is the difference between them?
The difference is that the latter invokes the NSCalendar class method currentCalendar. The code for the class method probably looks something like this:
[code]
…
static NSCalendar * __currentCalendar
static dispatch_once_t __currentCalendarOnce
…
(NSCalendar *)currentCalendar
{
// avoid race conditions
dispatch_once (&__currentCalendarOnce, ^ {
__currentCalendar = [[self alloc] initWith…];
});
return __currentCalendar;
}[/code]
Very good answer, and thank you very much~!
[quote=“ibex10”][quote]
The difference is that the latter invokes the NSCalendar class method currentCalendar. The code for the class method probably looks something like this:
[code]
…
static NSCalendar * __currentCalendar
static dispatch_once_t __currentCalendarOnce
…
(NSCalendar *)currentCalendar
{
// avoid race conditions
dispatch_once (&__currentCalendarOnce, ^ {
__currentCalendar = [[self alloc] initWith…];
});
return __currentCalendar;
}[/code][/quote][/quote]