01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Creating your own data types<br />

51<br />

Both type definitions say that struct box can also be referred to by the name cube.<br />

This allows you to declare variables in your application as follows:<br />

struct box a;<br />

cube b;<br />

The new data type cube created via the typedef statement is purely an element of syntactic<br />

sugar. As far as the <strong>Objective</strong>-C compiler is concerned, struct box and cube both<br />

mean the same thing: you’ve specified alternative names for the developer’s convenience.<br />

Although you can use typedefs to rename enums, structs, and unions, they can also<br />

be beneficial to provide alternative names for primitive data types such as int or<br />

double. One problem with the basic numeric data types is that they can sometimes<br />

seem meaningless in isolation. If you were presented with the following statement<br />

double x = 42;<br />

you wouldn’t be able to determine what the value 42 represents. Is it a temperature,<br />

weight, price, or count? Giving an existing data type a new name can make such a statement<br />

more self-documenting. As an example, listing 2.2 contains a type definition that<br />

can be found in the Core Location API, which is responsible for GPS positioning.<br />

Listing 2.2<br />

Example typedef from Core Location’s CLLocation.h header file<br />

// CLLocationDegrees<br />

// Type used to represent a latitude or longitude coordinate<br />

// in degrees under the WGS 84 reference frame. The degree can<br />

// be positive (North and East) or negative (South and West).<br />

typedef double CLLocationDegrees;<br />

Although the type definition provides an alternative name for double, it enables us to<br />

declare a variable using a statement similar to the following:<br />

CLLocationDegrees x = 42;<br />

This new statement, although essentially identical to the previous, makes the meaning<br />

of the value 42 much more apparent.<br />

Good variable names are equally as important<br />

Although the use of the typedef statement can provide more descriptive names to<br />

existing data types, this feature should not be relied on in isolation. For example, look<br />

at the given variable declaration:<br />

CLLocationDegrees x = 42;<br />

This could have arguably been made even more descriptive by using a better variable<br />

name instead of the typedef. For instance:<br />

double currentHeadingOfCar = 42;<br />

In general, try to be as descriptive as possible with any form of identifier, be that a<br />

variable, datatype, method, or argument name. The more self-documenting your<br />

code, the easier it is to maintain and come back to after periods of inactivity.

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

Saved successfully!

Ooh no, something went wrong!