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.

50 CHAPTER 2 Data types, variables, and constants<br />

HOW DO ARRAYS DIFFER FROM SIMPLE TYPES?<br />

Arrays behave a little bit differently from the data types we’ve discussed up to this<br />

point. For example, you may expect the following code snippet to print the result<br />

A=1, B=2, C=3 because of the assignment statement array2 = array1 setting the second<br />

array to the contents of the first:<br />

int array1[3] = { 1, 2, 3 };<br />

int array2[3] = { 4, 5, 6 };<br />

array2 = array1;<br />

NSLog(@"A=%d, B=%d, C=%d", array2[0], array2[1], array2[2]);<br />

But if you attempt to build this code sample, you should notice a compiler error complaining<br />

cryptically about “incompatible types in assignment.” What this error is<br />

attempting to convey is that the variable array2 can’t be used on the left-hand side of<br />

the assignment (=) operator: it can’t be assigned a new value, at least not in the manner<br />

shown here.<br />

In chapter 3, as we discuss the object-oriented features of <strong>Objective</strong>-C, we explore<br />

concepts such as pointers and the difference between value and reference types,<br />

which will help explain why <strong>Objective</strong>-C refuses to accept what on initial glance looks<br />

like a perfectly acceptable request.<br />

2.4.4 The importance of descriptive names<br />

<strong>Objective</strong>-C provides a way to declare an alternative name for an existing data type via<br />

a statement known as a type definition, or typedef for short. Typedefs can be useful<br />

when the built-in data type names aren’t descriptive enough or if you want to differentiate<br />

the purpose or intent of a variable from its physical data type (which you may<br />

decide to change at some point in time).<br />

You’ve already seen typedefs, although they weren’t pointed out as such: the NSInteger<br />

and NSUInteger data types discussed earlier are typedefs that map back to the<br />

built-in datatypes int and long int as required. This isn’t Apple-specific magic, simply<br />

a couple of lines of prespecified source code that automatically get included in every<br />

Cocoa Touch application.<br />

To declare your own typedefs, you use the typedef keyword followed by the name<br />

of an existing data type (or even the specification of an entirely new one) followed by<br />

the new name you want to associate with it.<br />

As an example, you could assign the alternative name cube to the struct box data<br />

type you previously declared by adding the following type definition:<br />

typedef struct box cube;<br />

Or you could merge the declaration of struct box into the typedef statement:<br />

typedef struct box {<br />

int width;<br />

int height;<br />

int depth;<br />

} cube;

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

Saved successfully!

Ooh no, something went wrong!