Difference between revisions of "Objective-C Enumerators"

From Techotopia
Jump to: navigation, search
m (Text replacement - "<google>ADSDAQBOX_FLOW</google>" to "<htmlet>adsdaqbox_flow</htmlet>")
Line 16: Line 16:
  
 
== Declaring an Enumeration ==
 
== Declaring an Enumeration ==
<google>ADSDAQBOX_FLOW</google>
+
<htmlet>adsdaqbox_flow</htmlet>
 
The syntax for declaring an enumeration is as follows:
 
The syntax for declaring an enumeration is as follows:
  

Revision as of 18:05, 1 February 2016

PreviousTable of ContentsNext
An Overview of Objective-C FunctionsAn Overview of the Objective-C Foundation Framework

<google>BUY_OBJC</google>

Objective-C Enumerators allow variables to be declared with a predefined list of valid values, each of which is assigned a name that can be used when setting the variable. This has the advantage of making the code easier to read and understand.


Contents


Why Use Enumerators

As previously described, enumerators allow the programmer to predefine the range of values that can be assigned to a variable and use self-explanatory names when setting those values. The benefits of this are twofold. Firstly this makes code easier to read and understand and secondly this, to a certain extent, allows control of the values that can be assigned (though as we will see later in this chapter, enforcement is rather weak).

Declaring an Enumeration

The syntax for declaring an enumeration is as follows:


enum <name> { <value name 1>, <value name 2>, ... };


In the above syntax outline, enum is the keyword that tells the compiler we are creating an enumeration, <name> is the name to be assigned to the enumeration and the <value name> fields are the names that can be used to set the variable. Internally, enumerators use numbers corresponding to the value names. By default the first value corresponds to 0, the second to 1 and so on. It is also possible to specify the values used by each value name:

enum <name> { <value name 1> = <value1>, <value name 2> = <value2>, ... };

For example, we can specify an enumeration for the days of the week:

enum daysofweek {monday, tuesday, wednesday, thursday, friday, saturday, sunday};

In the above construct, monday is equal to 0, tuesday is 1 and so on. If we specify a number for just one entry, the remaining values will follow on from that number. For example:

enum daysofweek {monday = 1, tuesday, wednesday, thursday, friday, saturday, sunday};

In this case, monday will be 1, tuesday will be 2 etc.

Alternatively we can specify a value for each entry in the enumeration:

enum temperature {cold = 5, warm = 50, hot = 95};

Creating and Using an Enumeration

Having declared an enumeration the next step is to declare a variable of that type. For example, to create a variable called currentTemp from our temperature enumerator type we would write the following code:

enum temperature currentTemp;

Now that we have created a variable based on our enumerator data type, we can try setting a value using one of the value names and then displaying the current value of the variable:

int currentTemp = hot;

NSLog (@"Current temperature is %i", currentTemp);

When compiled and executed, the above code will output the following:

2009-10-21 10:25:46.640 t[11525:10b] Current temperature is 95

As we can see, although we assigned hot to our currentTemp variable, Objective-C translated it to the corresponding number 95. If we attempt to assign a value using an undefined name as follows the code will fail to compile with a 'tepid' undeclared error message:

currentTemp = tepid;

We can, however, bypass this system by assigning a number directly to the variable:

currentTemp = 109;

The above code will, unfortunately, compile and run.

Enumerators and Variable Names

When using enumerators in Objective-C it is important to be aware that both the enumerator name and the value names are treated as symbols that must be unique within the given scope. For example, because we use the value name hot in our temperature enumerator we cannot then use hot as a variable name within the same scope (for information on variable scope refer to Objective-C Variable Scope and Storage Class). Take, for the example, the following code. Here we have our temperature enumerator and also a declaration for a variable named hot:

enum temperature {cold = 5, warm = 50, hot = 95};

enum temperature currentTemp;

int hot = 100;

int currentTemp = hot;

NSLog (@"Current temperature is %i", currentTemp);

If we attempt to compile this code, an error will result informing us that a duplicate symbol has been detected:

temp.m: In function 'main':
temp.m:14: error: 'hot' redeclared as different kind of symbol
temp.m:10: error: previous definition of 'hot' was here

<google>BUY_OBJC_BOTTOM</google>


PreviousTable of ContentsNext
An Overview of Objective-C FunctionsAn Overview of the Objective-C Foundation Framework