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.

206 CHAPTER 10 Error and exception handling<br />

[alert show];<br />

[alert release];<br />

message:message<br />

delegate:nil<br />

cancelButtonTitle:nil<br />

otherButtonTitles:@"OK", nil];<br />

Listing 10.2 first checks whether the error object has a userInfo dictionary and whether<br />

that dictionary has a value for the NSUnderlyingErrorKey. If so, it displays its localized-<br />

Description. If not, it displays the main error object’s localizedDescription.<br />

This technique doesn’t produce the best message for users in all cases. During<br />

development it’s always important to test for different error conditions and look at the<br />

contents of the userInfo dictionary because different frameworks and different subsystems<br />

provide different amounts and kinds of information in the NSError objects<br />

they create. There’s no one-size-fits-all solution to display error messages to your users.<br />

In most cases, you know what could go wrong in a certain piece of code. Use that<br />

knowledge to create and display the most helpful and user-friendly message possible.<br />

What if you want to create your own NSError objects? Let’s look at that next.<br />

10.2 Creating NSError objects<br />

Adopting Cocoa’s error-handling pattern in your own code is simple. You just need to<br />

accept a pointer to an NSError pointer as an argument to your method, check if one<br />

has been provided by the caller, and create an instance of NSError with the information<br />

describing the error if something went wrong.<br />

Let’s look at an example.<br />

10.2.1 Introducing RentalManagerAPI<br />

Pretend you have to build a framework that talks to the<br />

my-awesome-rental-manager.com website. You want to<br />

give this framework to third-party developers to use in<br />

their iOS projects, and naturally you want to build a beautiful<br />

API that handles errors gracefully. One of the framework’s<br />

methods takes a dictionary of values that represents<br />

a classified ad, which is used here for simplicity’s sake; in<br />

real life you’d use a custom class to represent such an ad.<br />

The method publishes the ad to the website after checking<br />

that all required values are present. If not, it creates<br />

and reports an error that indicates which fields are missing,<br />

as shown in figure 10.1.<br />

Since we’re concentrating on how to create and handle<br />

errors in this chapter, we won’t go into every detail of<br />

setting up a demo project or designing the user interface<br />

for this application in Interface Builder. If you want<br />

to use the techniques you’ve learned in the previous<br />

Figure 10.1 The<br />

RentalManagerAPI<br />

application displaying an error

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

Saved successfully!

Ooh no, something went wrong!