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.

204 CHAPTER 10 Error and exception handling<br />

10.1 NSError—handling errors the Cocoa way<br />

In Cocoa and Cocoa Touch, nearly all methods that could possibly fail for reasons you<br />

as a developer can’t control take a pointer to an NSError pointer as their last parameter.<br />

Why a pointer to a pointer? Because that way the called method can create an<br />

instance of NSError and you can access it afterwards through your NSError pointer.<br />

Before we go any further, let’s look at an example in the following listing.<br />

Listing 10.1<br />

Trying to load a nonexistent file<br />

NSError *error;<br />

NSData *data = nil;<br />

data = [[NSData dataWithContentsOfFile:@"i-dont-exist"<br />

options:NSDataReadingUncached<br />

error:&error] retain];<br />

if (data == nil) {<br />

NSLog(@"An error occurred: %@",<br />

[error localizedDescription]);<br />

} else {<br />

NSLog(@"Everything's fine");<br />

}<br />

This example is simple: you create an NSError pointer variable called error. Then you<br />

try to initialize an NSData object with a nonexistent file and pass in the address of your<br />

pointer variable (&error). Next you check whether or not you got a data object. This<br />

step is extremely important: when no error has occurred, the state of the passed-in<br />

error pointer is undefined! It’s unsafe to do anything with the error pointer unless<br />

you’re sure that an error did occur. That’s why you always need to check the return<br />

value of a method before accessing the error object.<br />

When you run this code, you’ll notice that the localized description of the error in<br />

listing 10.1 wouldn’t be very helpful to your users, though:<br />

"An error occurred: The operation couldn't be completed. (Cocoa error 260.)"<br />

Let’s look at NSError in more depth so you know how to get useful information from<br />

it to display to your users.<br />

10.1.1 Getting NSError to talk<br />

NSError objects have both an error domain and an error code. The domain exists<br />

mostly for historical reasons, but it provides useful information because it tells you<br />

which subsystem reported the error. The error domain is a string, and the Foundation<br />

framework declares four major error domains: NSMachErrorDomain, NSPOSIXError-<br />

Domain, NSOSStatusErrorDomain, and NSCocoaErrorDomain. Frameworks like Core Data<br />

have their own error domains. They provide namespaces for error codes or group them.<br />

Error codes are numeric and tell you which error occurred. The error codes for<br />

the different subsystems are usually defined in header files and can also be found in<br />

the online documentation (http://developer.apple.com/library/ios/navigation/). The

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

Saved successfully!

Ooh no, something went wrong!