0

In my app I want to let user set display style of a date. I've considered to just modify unitFlags to achieve that. I mean this

NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit;

But here's the question: How can I add or remove a NSCalendarUnit to this integer?

I'm using NSCalendar to get NSDateComponents from a date.

Sorry if the question is too stupid, I wasn't working enough with bitwise operations :(

Undo
  • 25,519
  • 37
  • 106
  • 129
Randex
  • 770
  • 7
  • 30

1 Answers1

5

I am not sure if this is what you are looking for, but you can add a flag using the "bitwise or" operator:

unitFlags |= NSYearCalendarUnit;

and remove a flag using "bitwise and" in combination with "bitwise complement";

unitFlags &= ~NSYearCalendarUnit;

To check for a flag:

if ((unitFlags & NSYearCalendarUnit) != 0) {
    // NSYearCalendarUnit is set
} else {
    // NSYearCalendarUnit is not set
}
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thanks for your answer, this is what I need. By the way, is there any way to check if there's a calendar unit in unitFlags? Or to get all units in the variable? – Randex Apr 30 '13 at 18:55