How can I view an NSError?

IphoneObjective CCocoaLogging

Iphone Problem Overview


What's the best way to log an NSError?

- (void)checkThing:(Thing *)thing withError:(NSError *)error {
    NSLog(@"Error: %@", error);
}

Gives me a null message

Iphone Solutions


Solution 1 - Iphone

Looking at the NSError documentation tells me that you'll need to do something like:

NSLog(@"%@",[error localizedDescription]);

This should then give you human readable output

Solution 2 - Iphone

> NSLog(@"Error: %@", error); > > Gives me a null message

Then error is nil, not an NSError instance.

Solution 3 - Iphone

Here's a rough method I use to log errors while developing; (Not for Cocoa-touch)

// Execute the fetch request put the results into array
NSError *error = nil;
NSArray *resultArray = [moc executeFetchRequest:request error:&error];
if (resultArray == nil)
{
    // Diagnostic error handling
    NSAlert *anAlert = [NSAlert alertWithError:error];
    [anAlert runModal];
}

NSAlert takes care of displaying the error.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionnevan kingView Question on Stackoverflow
Solution 1 - IphoneJamesView Answer on Stackoverflow
Solution 2 - IphonePeter HoseyView Answer on Stackoverflow
Solution 3 - IphoneAbizernView Answer on Stackoverflow