Using enum to define bit masks

Often you will want to define a list of constants, each representing an integer with one bit turned on. Then, these integers can be bitwise-ORed together and tested for using bitwise-AND, as described above.

The elegant way to do this is to define an enum that uses the left-shift operator to define the values. Here is how the constants for the UIDataDetector are defined:

e​n​u​m​ ​{​ ​ ​ ​ ​U​I​D​a​t​a​D​e​t​e​c​t​o​r​T​y​p​e​P​h​o​n​e​N​u​m​b​e​r​ ​ ​ ​=​ ​1​ ​<​<​ ​0​,​ ​ ​ ​ ​U​I​D​a​t​a​D​e​t​e​c​t​o​r​T​y​p​e​L​i​n​k​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​=​ ​1​ ​<​<​ ​1​,​ ​ ​ ​ ​U​I​D​a​t​a​D​e​t​e​c​t​o​r​T​y​p​e​A​d​d​r​e​s​s​ ​ ​ ​ ​ ​ ​ ​=​ ​1​ ​<​<​ ​2​,​ ​ ​ ​ ​U​I​D​a​t​a​D​e​t​e​c​t​o​r​T​y​p​e​C​a​l​e​n​d​a​r​E​v​e​n​t​ ...

Get Objective-C Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.