This concept seems to trouble me. Why does an NSError object need its pointer passed to a method that is modifying the object? For instance, wouldn’t just passing a reference to the error do the same thing?
NSError *anError;
[myObjc doStuff:withAnotherObj error:error];
and then in doStuff:
- (void)doStuff:(id)withAnotherObjc error:(NSError *)error
{
// something went bad!
[error doSomethingToTheObject];
}
Why doesn’t the above work like most other object messaging patterns work? Why must instead we use error:(NSError **)error?
The
NSError**pattern is used when a method normally returns some value but instead may need to return an error object (of typeNSError*) if it fails. In Objective-C a method can only return one type of object, but this is a case where you want to return two. In C-like languages when you need to return an extra value you ask for a pointer to a value of that type, so to return anNSError*you need anNSError**parameter. A more realistic example would be this:If you only had an
NSError*parameter instead of anNSError**thendoStuffwould never be able to pass the error object back to its caller.