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.

The basic data types<br />

35<br />

while growing to a 64-bit integer when compiling the same source code on a 64-bit<br />

platform. This allows code to take advantage of the increased range provided by 64-bit<br />

integers while not using excessive memory when targeting 32-bit systems.<br />

Eagled-eyed developers may wonder why new data types such as NSInteger were<br />

introduced when existing data types such as long int would appear to already fit the<br />

desired role. NSInteger exists for use in APIs that should be 64-bit integers on 64-bit<br />

platforms but for one reason or another must be typed as int instead of long int on<br />

32-bit platforms.<br />

Good habits learned now mean less hassle in the future<br />

Declaring your variables using data types such as NSInteger and NSUInteger can<br />

be considered a form of future-proofing your source code. Although they’re identical<br />

to int and unsigned int when compiling for the iPhone today, who knows what’s<br />

around the corner? Perhaps a future iPhone or iPad will be a 64-bit device, or you’ll<br />

want to reuse some of your source code in a matching desktop application.<br />

It’s much easier to establish the habit of using portable data types such as NSInteger<br />

or NSUInteger now (even if you don’t get much immediate benefit from it) than<br />

to have to correct such portability issues in the future.<br />

2.2.2 Filling in the gaps—floating-point numbers<br />

When modeling the real world, it’s common to come across numbers that contain a<br />

fractional part, such as 0.25 or 1234.56. An integer variable can’t store such values, so<br />

<strong>Objective</strong>-C provides alternative data types called float and double for storing this<br />

kind of data. As an example, you can declare a variable f of type float and assign it<br />

the value 1.4 as follows:<br />

float f = 1.4;<br />

Floating-point constants can also be expressed in scientific or exponential notation by<br />

using the character e to separate the mantissa from the exponent. The following two<br />

variable declarations are both initialized to the same value:<br />

float a = 0.0001;<br />

float b = 1e-4;<br />

The first variable is assigned the value 0.0001 via a familiar decimal constant. The second<br />

variable is assigned the same value but this time via a constant expressed in scientific<br />

notation. The constant 1e-4 is shorthand for 1 × 10 -4 , which, once calculated, produces<br />

the result 0.0001. The e can be thought of as representing “times 10 to the power of.”<br />

In <strong>Objective</strong>-C, floating-point variables are available via two main data types, which<br />

trade off the range of possible values they can represent and the amount of memory<br />

they use, as shown in table 2.3.<br />

You may wonder how a variable of type float or double can store such a wide<br />

range of values when a similar sized int can only store a much smaller range of values.<br />

The answer lies in how floating-point variables store their values.

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

Saved successfully!

Ooh no, something went wrong!