01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Creating your own data types<br />

45<br />

With what we’ve covered so far, the best approach to store such values would be to<br />

use an integer variable and to assign each state a unique value. For example, a value of 1<br />

could indicate the compass was pointing North, whereas a value of 2 could indicate it<br />

was pointing East. As a human, however, it can be rather tricky to manage this because<br />

you don’t tend to think in terms of numbers. Seeing a variable set to the value 2, you<br />

don’t immediately think “East.” <strong>Objective</strong>-C has a custom data type called an enumeration<br />

that’s designed to resolve this.<br />

To define a new enumerated data type, you use the special keyword enum. This is<br />

followed by a name and a list of possible values in curly braces. For example, you<br />

could declare an enumerated data type to store compass directions as follows:<br />

enum direction { North, South, East, West };<br />

Once the new enumeration data type is declared, you can create a variable of type<br />

enum direction and assign it the value North, as shown here:<br />

enum direction currentHeading = North;<br />

You may wonder what integer value North represents because you didn’t explicitly specify<br />

this during the declaration of the enumerated data type. By default, the first enumerated<br />

value (North) is given the integer value 0, and each value thereafter is given a<br />

value one greater than the name that preceded it. This convention can be overridden<br />

when the enumeration is declared. As an example, with the following declaration:<br />

enum direction { North, South = 10, East, West };<br />

North has the value 0 because it’s the first enumerated value. South has the value 10<br />

because it’s explicitly specified via an initializer, and East and West have the values 11<br />

and 12 respectively because they immediately follow South. It’s even possible for more<br />

than one name to map to the same integer value, although in this case there’s no way<br />

to tell the two values apart.<br />

In theory, an enumerated data type should be used to store only one of the values<br />

specified in the enumeration. Unfortunately, <strong>Objective</strong>-C won’t generate a warning if<br />

this rule is violated. As an example, the following is perfectly valid in an <strong>Objective</strong>-C<br />

program and in many cases won’t produce a compilation error or warning:<br />

enum direction currentHeading = 99;<br />

Even if this is possible, you shouldn’t rely on it. Try to restrict yourself to storing and<br />

comparing only the symbolic names you specified in the type declaration. Doing so<br />

allows you to easily change the values in a single place and have confidence that all<br />

logic in your application is correctly updated. If you make assumptions based on the<br />

value of an enumerated data type (or store random integer values), you defeat one of<br />

their main benefits, which is the association of a symbolic name to a specific value.<br />

The association of integer value and symbolic name has a number of benefits,<br />

including an improved debugging experience, as can be seen in figure 2.4.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!