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.

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

Unlike some languages with a true Boolean data type, <strong>Objective</strong>-C doesn’t restrict<br />

variables of type BOOL to storing only the two values YES and NO. Internally, the framework<br />

defines BOOL as another name for the signed char data type discussed previously.<br />

When <strong>Objective</strong>-C evaluates a Boolean expression, it assumes any nonzero value<br />

indicates true and a value of zero indicates false. This choice of storage formats can<br />

lead to some interesting quirks. For example, the following code snippet tries to suggest<br />

that a BOOL variable can store values of both true and false at the same time:<br />

BOOL result = 45;<br />

if (result) {<br />

NSLog(@"the value is true");<br />

}<br />

if (result != YES) {<br />

NSLog(@"the value is false");<br />

}<br />

This quirk occurs because the result variable stores a nonzero value (hence indicating<br />

truth) but doesn’t store exactly the same value that YES is defined as (1). In general,<br />

it’s best to compare BOOL values against the value NO. Because only one value can ever<br />

indicate false, you can be assured hard-to-spot errors such as the one demonstrated<br />

here don’t occur in your source code.<br />

This completes our initial coverage of how to declare and store values of built-in<br />

primitive data types. You still have much to learn about them, though, and we continue<br />

to discuss them throughout the rest of the book.<br />

2.3 Displaying and converting values<br />

Using variables to store values is useful, but at some stage, you’ll want to display them<br />

to the user. Usually this will necessitate converting a raw value into a nicer, more formatted<br />

form. For example, a floating-point calculation may result in the value<br />

1.000000000000276, but a user may not be interested in an answer to this level of precision<br />

(if indeed the result was ever that precise given the potential inaccuracies discussed<br />

in section 2.2.2). It may be more suitable to present this value only to two<br />

decimal places, as 1.00.<br />

In the following section, we discuss in detail how you can alter the display of arguments<br />

provided to a call to NSLog by altering what is called the format string. We also<br />

take a brief look at how numeric values can be converted between the various representations<br />

and how this too can affect the results of a calculation.<br />

2.3.1 NSLog and Format Specifiers<br />

The first argument provided in a call to NSLog specifies what is called the format string.<br />

NSLog processes this string and displays it in the Xcode debugger console. In most<br />

cases, the format string contains one or more placeholders that are indicated by a %<br />

character. If placeholders are present, NSLog expects to be passed a matching number<br />

of additional arguments. As NSLog emits its message, it substitutes each placeholder

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

Saved successfully!

Ooh no, something went wrong!