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.

208 CHAPTER 10 Error and exception handling<br />

}<br />

if (anError != NULL) {<br />

NSString *description = @"The ad could not be \<br />

published because some required \<br />

values are missing.";<br />

NSString *recoverySuggestion = @"Please provide \<br />

the missing values and try again.";<br />

NSArray *keys = [NSArray arrayWithObjects:<br />

NSLocalizedDescriptionKey,<br />

NSLocalizedRecoverySuggestionErrorKey,<br />

RMAMissingValuesKey, nil];<br />

NSArray *values = [NSArray arrayWithObjects:<br />

description,<br />

recoverySuggestion,<br />

missingValues, nil];<br />

NSDictionary *userDict = [NSDictionary<br />

dictionaryWithObjects:values<br />

forKeys:keys];<br />

*anError = [[[NSError alloc] initWithDomain:<br />

RMAErrorDomain<br />

code:RMAValidationError<br />

userInfo:userDict] autorelease];<br />

}<br />

return NO;<br />

} else {<br />

return YES;<br />

}<br />

Caller<br />

interested<br />

in errors?<br />

Create an<br />

instance of<br />

NSError<br />

@end<br />

Let’s examine the code. In the header file, some constants are set up: a custom error<br />

domain, some special keys for the userInfo dictionary, and some error codes. Using<br />

constants and enumerators for these things makes your code much more readable<br />

and maintainable. In the implementation of the publishAd:error: method, you’ll<br />

notice one interesting thing right away: the use of the @throw directive. We briefly<br />

look at exceptions in the next section of this chapter, but here you see one common<br />

use case for them: catching programming errors. Your method should never be called<br />

without an instance of NSDictionary, and with this exception you make sure that such<br />

an error gets caught during development.<br />

The rest of the method isn’t rocket science: it checks if any required values are<br />

missing and collects them in an array. If some values are missing, you create a description<br />

and some recovery suggestions and put them in a dictionary together with the array<br />

of missing values. With that, you create an instance of NSError and have the pointer to<br />

a pointer (anError) point to it by using the asterisk in front of the variable name<br />

(*anError). That tells the compiler that you want to change the value of the pointer<br />

that anError is pointing to, not the value of anError itself. This is a little confusing, no<br />

doubt, but you’ll wrap your head around it in no time.<br />

Finally you return NO when an error occurred and YES otherwise. Now how do you<br />

call this method and handle the error it might return?

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

Saved successfully!

Ooh no, something went wrong!