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.

72 CHAPTER 3 An introduction to objects<br />

The hardest challenge in providing images for each rental property in the list is<br />

determining in which city each property is located. At this stage, you’ll be fairly naive<br />

and split the address field on the sole semicolon it contains. In the future, you may<br />

like to revisit this solution and find a more robust mechanism or use some of the<br />

iPhone SDK geocoding APIs.<br />

Using the rangeOfString: message available on the NSString class, b you can<br />

determine the location of the semicolon in the property’s address, and by passing this<br />

index into additional NSString methods, substringFromIndex: and substringTo-<br />

Index:, you can separate the street address and city into two separate strings c. In<br />

the call to substringFromIndex: you have to add 2 to the index returned by rangeOf-<br />

String: to skip over the semicolon and the space that follows it.<br />

Now that you’ve extracted a rental property’s city from its address, you’re ready to<br />

determine which image to display and attach it to the table view cell d. You do this by<br />

comparing the contents of the city variable against a couple of hardcoded city<br />

names. Once you determine the type of location, use UIImage’s imageNamed functionality<br />

to load the images you previously included in the application.<br />

One obvious problem with the technique demonstrated in this revision of the Rental<br />

Manager application is that the list of cities and their mappings to location types is hardcoded<br />

and specified in code. This makes it difficult and more time consuming (especially<br />

with the App Store submission process) to update the behavior of the application<br />

as your rental portfolio expands and covers properties in other cities. Ideally, you want<br />

to separate the logic from the data so you can easily update the city mappings without<br />

needing to recompile or to resubmit the application: more on this in the next chapter.<br />

3.7 Summary<br />

Enhancing the procedural C language to have object-oriented features is essentially<br />

what brought <strong>Objective</strong>-C to life. The benefits of developing applications in an objectoriented<br />

manner generally far outweigh the extra effort required to learn the additional<br />

terminology and techniques that object-orientation entails.<br />

Chief among the advantages of object-oriented programming is an improved ability<br />

to separate a complex application into a number of smaller, discrete building<br />

blocks, or classes. Rather than considering a large, complex system, the developer’s<br />

task becomes one of developing multiple smaller systems that combine and build on<br />

top of each other to perform tasks far more complex than any one part can do alone.<br />

The ability to package data plus logic into modules, called classes, also makes it<br />

easier to transplant an object designed and developed in one application into<br />

another. Application frameworks such as Cocoa Touch and Foundation Kit take this to<br />

another level. Their sole purpose is to provide developers with a large number of<br />

classes out of the box and ready to be put to work in their applications. Developers<br />

need not spend time ironing out bugs and quirks in the 900th string concatenation<br />

implementation the world has seen. Application developers can instead focus on the<br />

distinct features of their own applications.

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

Saved successfully!

Ooh no, something went wrong!