Question

I can't seem to find an answer to this. Here nor documentation. Maybe I'm not sure what I should be looking for?

unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;

From my understanding those should be logical bitwise operators for "OR"

How do they work in this situation?

Was it helpful?

Solution

bitwise operators used like this is a way of setting multiple boolean flags in one variable, called a bitmask

so if

FlagA = 1 << 3; //binary = 1000
FlagB = 1 << 2; //binary = 0100
FlagC = 1 << 1; //binary = 0010
FlagD = 1 << 0; //binary = 0001

and i set

myFlag = FlagA | FlagC | FlagD;

it is the or of

1000 |
0010 |
0001 =
1011

So in your example you are setting the unit flags to be Hour, Minute, Day and Month

OTHER TIPS

You are setting a bitmask for the NSDateComponents with NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit.

So basicly your are saying you want to use all the parameters. It is useful if you want to give a method multiple flags in one variable.

NSCalendar is pretty expensive class and computation of some units can take long time. So, with these flags you can specify what units do you want to calculate to make it faster. Units not specified there will not be calculated.

Also when you do use NSCalendar, cache it, it's really expensive to create it again, again and again.

Rest was answered by @wattson12.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top