Error arguments in Objective-C
From the Programming with Objective-C (backup) overview from Apple:
When dealing with errors passed by reference, it’s important to test the return value of the method to see whether an error occurred, as shown above. Don’t just test to see whether the error pointer was set to point to an error.
and from the Error Handling Programming Guide (backup):
Success or failure is indicated by the return value of the method. Although Cocoa methods that indirectly return error objects in the Cocoa error domain are guaranteed to return such objects if the method indicates failure by directly returning
nil
orNO
, you should always check that the return value isnil
orNO
before attempting to do anything with theNSError
object.
For example, let’s say we’re executing a fetch request:
NSError *error = nil;
NSFetchRequest *request = /* … */;
NSArray *objects = [context executeFetchRequest:request
error:&error];
To test if the fetch was successful, we must do:
if (objects) {
// hooray!
} else {
NSLog(@"Got an error: %@", error);
}
A method taking an NSError **
does not guarantee how it is used when successful. In this case, even a successful method call may end up with error
set to something other than nil
.