Stephen J Stephen J - 3 months ago 32
Objective-C Question

Did Objective C start using + instead of | for combining bits?

Anytime you combine two flags or more, you use |. Except Swift, which changed it to use array syntax. But now I am staring at some documentation on local notifications that does this:

requestAuthorizationWithOptions: (UNAuthorizationOptionAlert + UNAuthorizationOptionSound)

Where those are enums. I can't find where the + operator suddenly started working in this manner. I don't see any indicators that | couldn't just be used instead. I realize that maybe the enums themselves could just happen to work by the nature of their bits and how they add, but I doubt my last statement.


So, to codify the discussion in comments above: no, + is not a replacement for |. a + b == a | b if and only if a and b have bitpatterns which share no overlapping bits; in the cases of option enums, this is often true (because it is useful to represent options in this way), but not guaranteed. This is the case that you see above, though good code will be a bit more explicit (and readable) by using bitwise or.