I got into the habit of coding my error handling this way:
NSError* error = nil;
NSDictionary *attribs = [[NSFileManager defaultManager] removeItemAtPath:fullPath error:&error];
if (error != nil) {
DLogErr(@"Unable to remove file: error %@, %@", error, [error userInfo]);
return;
}
But looking at the documentation It seems like I got this wrong.:
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error
If an error occurs, upon return contains an NSError object that describes the problem. Pass NULL if you do not want error information.
Technically there is no difference between nil and NULL so does this mean I'm actually turning this off and will never get a error message (even if the delete in the above example did fail) ? Is there a better way to code this ?
Thanks.
No I do it the same way and it works just fine for detecting errors. You are not passing NULL to it you are passing a pointer to NULL to it which is a very different thing. Although another option you might want to add is.
Passing
NULL
means the following:i.e., the
error
parameter isNULL
. Internally,-removeItemAtPath:error:
sees if a valid pointer was passed. If it’sNULL
, it simply won’t report the error as anNSError
instance — but the return value will indicate whether the method completed successfully.Also, your test is wrong. You shouldn’t be using the
error
output parameter to detect if an error occurred because it might be set even if the method completes successfully. Instead, you should use the return value of the method to detect errors. If the return value (in this particular case) isNO
, then use theerror
output parameter to get information about the error:Quoting the Error Handling Programming Guide,
Edit: As NSGod pointed out,
-removeItemAtPath:error:
returnsBOOL
, notNSDictionary *
. I’ve edited my answer to reflect that as well.First off, the following line doesn't really make sense:
-removeItemAtPath:error:
returns a BOOL value, not a dictionary.I think I see what you’re wondering about with the
NULL
value. Notice carefully though, how there are 2 *'s in the error parameter in the method signature:That means a pointer to a pointer. When you pass in
&error
, you are passing in the address of the pointer to theNSError
. (Ugh, someone else can probably help me out here, as my head still starts to swim when dealing with pointers to pointers). In other words, even though you have seterror
tonil
, you aren't passing inerror
to the method, you're passing in&error
.So, here’s what the re-written method should look like: