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.

254 CHAPTER 12 Reading and writing application data<br />

UIAlertView and then use the abort() function to exit the application. Apple discourages<br />

the use of the abort() function and asks developers to display a message that<br />

tells the user to quit the application by pressing the Home button. While this works too,<br />

the abort() function has one advantage: it generates a crash log. The log can be<br />

extremely useful for tracking down the problem. It’s up to you to decide how you’ll handle<br />

fatal errors, but abort() isn’t as bad as it’s made out to be if you use it responsibly.<br />

Validation errors must also be dealt with: a required value is missing, a string is too<br />

short, or a number is too large. Such errors can be recovered from, and you should<br />

tell the user about them and the user a chance to fix them.<br />

Listing 12.18 shows one way to handle and display validation errors (for example,<br />

in PersonDetailViewController). Please note that you should adapt this method to<br />

your application and your user’s needs. As it stands, the messages aren’t quite user<br />

friendly but are definitely better than just saying “Saving failed.”<br />

Listing 12.18<br />

One way to handle and display validation errors<br />

- (void)displayValidationError:(NSError *)anError { Make sure it’s a built-in error<br />

if (anError &&<br />

[[anError domain] isEqualToString:@"NSCocoaErrorDomain"]) {<br />

NSArray *errors = nil;<br />

if ([anError code] == NSValidationMultipleErrorsError) {<br />

errors = [[anError userInfo] objectForKey:NSDetailedErrorsKey];<br />

} else {<br />

errors = [NSArray arrayWithObject:anError];<br />

}<br />

if (errors && [errors count] > 0) {<br />

NSString *messages = @"Reason(s):\n";<br />

for (NSError *error in errors) {<br />

NSString *entityName =<br />

[[[[error userInfo]<br />

objectForKey:@"NSValidationErrorObject"]<br />

entity]<br />

name];<br />

NSString *attributeName =<br />

Iterate over<br />

the errors<br />

[[error userInfo] objectForKey:@"NSValidationErrorKey"];<br />

NSString *msg;<br />

Extract multiple errors;<br />

wrap single errors<br />

Get attribute<br />

name<br />

Get entity<br />

name<br />

switch ([error code]) {<br />

Build appropriate<br />

case NSManagedObjectValidationError:<br />

error messages<br />

msg = @"Generic validation error.";<br />

break;<br />

case NSValidationMissingMandatoryPropertyError:<br />

msg = [NSString stringWithFormat:<br />

@"The attribute '%@' mustn't be empty.",<br />

attributeName];<br />

break;<br />

case NSValidationRelationshipLacksMinimumCountError:<br />

msg = [NSString stringWithFormat:<br />

@"The relationship '%@' doesn't have enough entries.",

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

Saved successfully!

Ooh no, something went wrong!