Reputation: 32207
I have an error getting passed by reference to some code I found online. The error comes back as an empty object meaning there was no error.
if I check for error.code
I get a bad access because the object is empty.
if I check for error == nil
I get a false because error
is an empty object.
How can I use logic to find that the error exists, but is empty?
Upvotes: 0
Views: 1405
Reputation: 15213
Errors are usually of type NSError
or a subclass of it. They are passed as references in methods declared this way:
-(void)DoSomeStuff:(NSError **)error;
So, when you call a method that requires you to pass a reference to a NSError
you call it this way:
NSError *error = nil;
[self DoSomeStuff:&error];
When this method finished its work you check if the error object has actually filled with something:
if(error)
{
//Do some stuff if there is an error.
//To see the human readable description you can:
NSLog(@"The error was: %@", [error localizedDescription]);
//To see the error code you do:
NSLog(@"The error code: %d", error.code);
}
else //There is no error you proceed as normal
{
//Do some other stuff - no error
}
P.S. If you get no error and the method does not behave as expected then there is something wrong with the implementation with this method. Especially if it's an open source stuff, coding mistakes can easily appear, so you can take a look at what the method does, debug and even fix if something is wrong...
Upvotes: 7