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.

Sample application<br />

69<br />

To append one string onto the end of another, you can use the stringByAppending-<br />

String: message:<br />

NSString *str2 = [str1 stringByAppendingString:@"Cool!"];<br />

This statement appends "Cool!" to the end of str1 and stores the result in str2. You<br />

can’t state str2 = str1 + @"Cool!" for reasons that will soon become clear.<br />

Now that you know how to create and manipulate string objects, it’s only natural to<br />

want to compare one against another to determine if they’re identical.<br />

3.5.4 Comparing strings<br />

To compare two string variables against each other, you may come up with a code snippet<br />

such as the following based on what you’ve learned about pointers in this chapter:<br />

NSString *str1 = [NSString stringWithFormat:@"Hello %@", @"World"];<br />

NSString *str2 = [NSString stringWithFormat:@"Hello %@", @"World"];<br />

if (str1 == str2) {<br />

NSLog(@"The two strings are identical");<br />

}<br />

Surprisingly, when this code is executed, you’ll notice it indicates that the two freshly<br />

created strings aren’t equal! To understand why, you need to realize that the two variables<br />

str1 and str2 are both simple C-style pointers. With its C-based roots, the ==<br />

operator checks for equality by determining if both variables point to the same piece<br />

of memory. Because you create two separate string instances, the two pointers point to<br />

distinct locations, and the expression evaluates to false. That both strings contain an<br />

identical sequence of characters is irrelevant because the == operator only considers<br />

the string’s memory location.<br />

To work around this problem, <strong>Objective</strong>-C, like most object-oriented languages<br />

with pointers, provides a message to compare the contents of an object instead of its<br />

location in memory. In <strong>Objective</strong>-C this message is called isEqual:<br />

if ([str1 isEqual:str2]) {<br />

NSLog(@"The two strings are identical");<br />

}<br />

It’s for a similar reason that the + operator can’t be used for string concatenation: it’s<br />

already used with C-style pointers for pointer arithmetic.<br />

3.6 Sample application<br />

To put the concepts you’ve learned in this chapter into practice, let’s expand the Rental<br />

Manager application to make greater use of the services provided by NSString to search<br />

and modify string content. Figure 3.5 shows how the application will look once you finish<br />

this round of modifications. Each rental property has an image beside it that categorizes<br />

the property’s location, such as near the sea, in the city, or in an alpine setting.<br />

To display images in your iPhone application, the obvious first step is to include<br />

some image resources in your project. To add images to the Xcode project, drag and

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

Saved successfully!

Ooh no, something went wrong!