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.

The basic data types<br />

33<br />

Because variables of type int are signed by default, it’s uncommon to see the signed<br />

qualifier used in most applications—its use is somewhat redundant.<br />

Once you declare a variable, you can assign it a value with the assignment operator,<br />

which is represented by an equals sign. For example, the following statements declare<br />

a new variable called a and then assign it the value 15.<br />

int a;<br />

a = 15;<br />

Because it’s common to declare a variable and then assign it an initial value, both<br />

statements can be combined:<br />

int a = 15;<br />

The value 15 used in this assignment statement is called a constant. A constant is any<br />

value that can never change its value while the application is running. A constant<br />

doesn’t have to be a single number; for example, the following variable declaration<br />

also makes use of a constant value.<br />

int a = 5 + 3;<br />

The value calculated by the expression 5 + 3 can never result in a number other than 8.<br />

The <strong>Objective</strong>-C compiler calculates the value of this expression during compilation<br />

and replaces it with a single constant value.<br />

By default, integer constants are specified in decimal, or base 10, which is the most<br />

familiar notation for most people. It’s also possible to specify integer constants in a<br />

number of other bases by using a special prefix in front of the number, as detailed in<br />

table 2.1.<br />

Table 2.1 Different ways to express the value 15 as an integer constant. Each format is identified by a<br />

special prefix that precedes the number.<br />

Name Base Prefix Example constant<br />

Octal 8 0 017<br />

Decimal 10 — 15<br />

Hexadecimal 16 0x 0x0F<br />

One trap new developers occasionally make is to include a leading zero at the start of<br />

a decimal number. As far as <strong>Objective</strong>-C is concerned, 017 isn’t the same value as 17.<br />

The leading zero in front of the first constant means the number is interpreted as an<br />

octal (base 8) number and hence equals the decimal value 15.<br />

FACING THE LESS-THAN-IDEAL REAL WORLD<br />

The iPhone is a vast improvement over the hardware and memory constraints of a traditional<br />

cell phone, but it’s still constrained by real-world realities such as a fixed<br />

amount of memory being available to applications. In an ideal world, developers

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

Saved successfully!

Ooh no, something went wrong!