enum

Often you will need to define a set of constants. For example, imagine that you were developing a blender with five speeds: Stir, Chop, Liquefy, Pulse, and Ice Crush. Your class Blender would have a method called setSpeed:. It would be best if the type indicated that only one of the five speeds was allowed. To do this, you would define an enumeration:

e​n​u​m​ ​B​l​e​n​d​e​r​S​p​e​e​d​ ​{​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​S​t​i​r​ ​=​ ​1​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​C​h​o​p​ ​=​ ​2​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​L​i​q​u​i​f​y​ ​=​ ​5​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​P​u​l​s​e​ ​=​ ​9​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​I​c​e​C​r​u​s​h​ ​=​ ​1​5​
}​;​

@​i​n​t​e​r​f​a​c​e​ ​B​l​e​n​d​e​r​ ​:​ ​N​S​O​b​j​e​c​t​
{​
 ​ ​ ​ ​/​/​ ​s​p​e​e​d​ ​m​u​s​t​ ​b​e​ ​o​n​e​ ​o​f​ ​t​h​e​ ​f​i​v​e​ ​s​p​e​e​d​s​
 ​ ​ ​ ​e​n​u​m​ ​B​l​e​n​d​e​r​S​p​e​e​d​ ​s​p​e​e​d​;​
}​

/​/​ ​s​e​t​S​p​e​e​d​:​ ​e​x​p​e​c​t​s​ ​o​n​e​ ​o​f​ ​t​h​e​ ​f​i​v​e​ ​s​p​e​e​d​s​
-​ ​(​v​o​i​d​)​s​e​t​S​p​e​e​d​:​(​e​n​u​m​ ​B​l​e​n​d​e​r​S​p​e​e​d​)​x​;​
@​e​n​d​

Developers get tired of typing enum BlenderSpeed, so they often use typedef to create a shorthand for it:

t​y​p​e​d​e​f​ ​e​n​u​m​ ​ ​{​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​S​t​i​r​ ​=​ ​1​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​C​h​o​p​ ​=​ ​2​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​L​i​q​u​i​f​y​ ​=​ ​5​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​P​u​l​s​e​ ​=​ ​9​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​I​c​e​C​r​u​s​h​ ​=​ ​1​5​
}​ ​B​l​e​n​d​e​r​S​p​e​e​d​;​

@​i​n​t​e​r​f​a​c​e​ ​B​l​e​n​d​e​r​ ​:​ ​N​S​O​b​j​e​c​t​
{​
 ​ ​ ​ ​/​/​ ​s​p​e​e​d​ ​m​u​s​t​ ​b​e​ ​o​n​e​ ​o​f​ ​t​h​e​ ​f​i​v​e​ ​s​p​e​e​d​s​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​ ​s​p​e​e​d​;​
}​

/​/​ ​s​e​t​S​p​e​e​d​:​ ​e​x​p​e​c​t​s​ ​o​n​e​ ​o​f​ ​t​h​e​ ​f​i​v​e​ ​s​p​e​e​d​s​
-​ ​(​v​o​i​d​)​s​e​t​S​p​e​e​d​:​(​B​l​e​n​d​e​r​S​p​e​e​d​)​x​;​
@​e​n​d​

Often you will not care what numbers the five speeds represent – only that they are different from each other. You can leave out the values, and the compiler will make up values for you:

t​y​p​e​d​e​f​ ​e​n​u​m​ ​ ​{​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​S​t​i​r​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​C​h​o​p​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​L​i​q​u​i​f​y​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​P​u​l​s​e​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​I​c​e​C​r​u​s​h​
}​ ​B​l​e​n​d​e​r​S​p​e​e​d​;​

Starting with OS X 10.8 and iOS 6, Apple introduced a new enum declaration syntax: NS_ENUM(). Here is what your enum looks like using this syntax:

t​y​p​e​d​e​f​ ​N​S​_​E​N​U​M​(​i​n​t​,​ ​B​l​e​n​d​e​r​S​p​e​e​d​)​ ​{​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​S​t​i​r​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​C​h​o​p​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​L​i​q​u​i​f​y​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​P​u​l​s​e​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​I​c​e​C​r​u​s​h​
}​;​

NS_ENUM() is actually a preprocessor macro that takes two arguments: a data type and a name.

Apple has adopted NS_ENUM() for enum declarations. The most important advantage of NS_ENUM() over the other syntax is the ability to declare the integral data type that the enum will represent (short, unsigned long, etc.).

With the old syntax, the compiler would choose an appropriate data type for the enum, usually int. If your enum will only have four options whose values do not matter, you do not need four bytes to store it; one byte will represent integral numbers up to 255 just fine. Recalling from Chapter 3 that a char is a one-byte integer, you can declare a space-saving enum:

t​y​p​e​d​e​f​ ​N​S​_​E​N​U​M​(​c​h​a​r​,​ ​B​l​e​n​d​e​r​S​p​e​e​d​)​ ​{​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​S​t​i​r​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​C​h​o​p​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​L​i​q​u​i​f​y​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​P​u​l​s​e​,​
 ​ ​ ​ ​B​l​e​n​d​e​r​S​p​e​e​d​I​c​e​C​r​u​s​h​
}​;​
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset