Which is better?
static unsigned unitFlags;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
});
OR is that just as efficient as writing
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
Or is there some other patterns more efficient? Would a define be better for ‘unitFlags’?
You should just do:
You only need to do the
dispatch_oncetrick if the expression on the right isn’t a valid compile-time constant. But this is a valid compile-time constant, so you don’t have to go through that slight of hand.But the
staticexpression is generally better than a#define(it’s typed, and for some complicated expressions, it can be more efficient). Stick with thestatic, in my opinion.