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.

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

wouldn’t have any constraint on the value of integers—they could be infinitely high or<br />

low—but, unfortunately, constraints do exist. Declaring a variable of type int allocates<br />

a set amount of memory in which to store the value, and hence it can represent only a<br />

limited range of values. If you wanted to store infinitely large values, the variable<br />

would also require an infinite amount of memory, and that’s clearly not possible.<br />

This is one reason for having unsigned and signed qualifiers. Using the unsigned<br />

qualifier trades off the ability to store negative numbers with the ability to double the<br />

range of positive values you can store in the same amount of memory. Other qualifiers<br />

include short and long, which can be added to an int data type to expand or contract<br />

the size of the variable. The most common sizes are listed in table 2.2.<br />

Table 2.2 Common integer data types. Various modifiers can be used to alter the size of a variable and<br />

hence the valid range of values that can safely be stored in them.<br />

Data type Size (bits) Unsigned range Signed range<br />

short int 16 0—65,535 –32,768—32,767<br />

int 32 0—4,294,967,295 –2,147,483,648—2,147,483,647<br />

long int 32 0—4,294,967,295 –2,147,483,648—2,147,483,647<br />

long long int 64 0—(2 64 – 1) –2 63 —(2 63 – 1)<br />

In <strong>Objective</strong>-C, int is the default data type for variables and parameters. This means<br />

you can remove the keyword int from your variable declaration statement and,<br />

in most cases, it will still compile. Therefore, the following two variable declarations<br />

are equivalent:<br />

unsigned a;<br />

unsigned int a;<br />

The first variable declaration implicitly implies the presence of the data type int by<br />

the presence of the unsigned qualifier. In the second declaration, the int data type is<br />

said to be explicitly specified because it’s present in the declaration.<br />

THE LEGACY OF CONTINUAL PROGRESS—NSINTEGER, NSUINTEGER, AND THEIR ILK<br />

As you explore Cocoa Touch, you’ll find that most APIs use data types with names such<br />

as NSInteger or NSUInteger instead of int and unsigned int. These additional types<br />

are part of Apple’s progress toward 64-bit computing.<br />

Currently, all iOS-powered devices (and older versions of Mac OS X) use a programming<br />

model called ILP32, which supports a 32-bit address space. Since Mac OS X 10.4,<br />

the desktop has been moving toward a different programming model, LP64, which supports<br />

a 64-bit address space. Under the LP64 model, variables of type long int and memory<br />

addresses are increased to 64 bits in size (compared to the 32 bits shown in<br />

table 2.2), whereas the size of all other primitive types, such as int, remain the same.<br />

As part of the effort to take full advantage of 64-bit platforms, Cocoa introduced the<br />

NSInteger data type to provide a data type that was a 32-bit integer on 32-bit platforms

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

Saved successfully!

Ooh no, something went wrong!