Creating Error Objects

+ errorWithDomain:code:userInfo:

Creates and initializes an NSError object for a given domain and code with a given userInfo dictionary.

Example

NSError *error = [NSError errorWithDomain:@"com.eezytutorials.iosTuts" code:200 userInfo:@{@"Error reason": @"Invalid Input"}];
NSLog(@"%@",error);
		

Output

2014-04-12 05:31:59.975 iOS-Tutorial[1540:a0b] Error Domain=com.eezytutorials.iosTuts Code=200 "The operation couldn’t be completed. (com.eezytutorials.iosTuts error 200.)" UserInfo=0xa08bb00 {Error reason=Invalid Input}
		

- initWithDomain:code:userInfo:

Returns an NSError object initialized for a given domain and code with a given userInfo dictionary.

Example

NSError *error = [[NSError alloc] initWithDomain:@"com.eezytutorials.iosTuts" code:200 userInfo:@{@"Error reason": @"Invalid Input"}];
NSLog(@"%@",error);
		

Output

2014-04-12 05:32:58.225 iOS-Tutorial[1559:a0b] Error Domain=com.eezytutorials.iosTuts Code=200 "The operation couldn’t be completed. (com.eezytutorials.iosTuts error 200.)" UserInfo=0x8db1550 {Error reason=Invalid Input}
		

Getting Error Properties

- code

Returns the receiver’s error code.

Example

NSError *error = [[NSError alloc] initWithDomain:@"com.eezytutorials.iosTuts" code:200 userInfo:@{ NSLocalizedFailureReasonErrorKey:@"LocalizedFailureReason"
        }];
NSLog(@"%d",[error code]);
		

Output

2014-04-12 05:42:56.399 iOS-Tutorial[1699:a0b] 200
		

- domain

Returns the receiver’s error domain.

Example

NSError *error = [[NSError alloc] initWithDomain:@"com.eezytutorials.iosTuts" code:200 userInfo:@{@"Error reason": @"Invalid Input"}];
NSLog(@"%@",error.domain);
		

Output

2014-04-12 05:33:32.224 iOS-Tutorial[1572:a0b] com.eezytutorials.iosTuts
		

- userInfo

Returns the receiver's user info dictionary.

Example

NSError *error = [[NSError alloc] initWithDomain:@"com.eezytutorials.iosTuts" code:200 userInfo:@{@"Error reason": @"Invalid Input"}];
NSLog(@"%@",error.userInfo);
		

Output

2014-04-12 05:33:54.333 iOS-Tutorial[1585:a0b] {
    "Error reason" = "Invalid Input";
}
		

Getting a Localized Error Description

- localizedDescription

Returns a string containing the localized description of the error.

Example

NSError *error = [[NSError alloc] initWithDomain:@"com.eezytutorials.iosTuts" code:200 userInfo:@{@"Error reason": @"Invalid Input"}];
NSLog(@"%@",[error localizedDescription]);
		

Output

2014-04-12 05:34:28.158 iOS-Tutorial[1597:a0b] The operation couldn’t be completed. (com.eezytutorials.iosTuts error 200.)		

- localizedRecoveryOptions

Returns an array containing the localized titles of buttons appropriate for displaying in an alert panel.

Example

NSError *error = [[NSError alloc] initWithDomain:@"com.eezytutorials.iosTuts" code:200 userInfo:@{ NSLocalizedFailureReasonErrorKey:@"LocalizedFailureReason",
        NSLocalizedDescriptionKey:@"LocalizedDescription",
        NSLocalizedRecoverySuggestionErrorKey:@"LocalizedRecoverySuggestion",
        NSLocalizedRecoveryOptionsErrorKey:@"LocalizedRecoveryOptions",
        NSRecoveryAttempterErrorKey:@"RecoveryAttempter",
        NSHelpAnchorErrorKey:@"HelpAnchor",
        NSStringEncodingErrorKey:@"NSStringEncodingError",
        NSURLErrorKey:@"NSURLError",
        NSFilePathErrorKey:@" NSFilePathError"
        }];
NSLog(@"%@",[error localizedRecoveryOptions]);
		

Output

2014-04-12 05:40:40.918 iOS-Tutorial[1638:a0b] LocalizedRecoveryOptions
		

- localizedRecoverySuggestion

Returns a string containing the localized recovery suggestion for the error.

Example

NSError *error = [[NSError alloc] initWithDomain:@"com.eezytutorials.iosTuts" code:200 userInfo:@{ NSLocalizedFailureReasonErrorKey:@"LocalizedFailureReason",
        NSLocalizedDescriptionKey:@"LocalizedDescription",
        NSLocalizedRecoverySuggestionErrorKey:@"LocalizedRecoverySuggestion",
        NSLocalizedRecoveryOptionsErrorKey:@"LocalizedRecoveryOptions",
        NSRecoveryAttempterErrorKey:@"RecoveryAttempter",
        NSHelpAnchorErrorKey:@"HelpAnchor",
        NSStringEncodingErrorKey:@"NSStringEncodingError",
        NSURLErrorKey:@"NSURLError",
        NSFilePathErrorKey:@" NSFilePathError"
        }];
NSLog(@"%@",[error localizedRecoverySuggestion]);
		

Output

2014-04-12 05:41:04.990 iOS-Tutorial[1653:a0b] LocalizedRecoverySuggestion
		

- localizedFailureReason

Returns a string containing the localized explanation of the reason for the error.

Example

NSError *error = [[NSError alloc] initWithDomain:@"com.eezytutorials.iosTuts" code:200 userInfo:@{ NSLocalizedFailureReasonErrorKey:@"LocalizedFailureReason",
        NSLocalizedDescriptionKey:@"LocalizedDescription",
        NSLocalizedRecoverySuggestionErrorKey:@"LocalizedRecoverySuggestion",
        NSLocalizedRecoveryOptionsErrorKey:@"LocalizedRecoveryOptions",
        NSRecoveryAttempterErrorKey:@"RecoveryAttempter",
        NSHelpAnchorErrorKey:@"HelpAnchor",
        NSStringEncodingErrorKey:@"NSStringEncodingError",
        NSURLErrorKey:@"NSURLError",
        NSFilePathErrorKey:@" NSFilePathError"
        }];
NSLog(@"%@",[error localizedFailureReason]);
		

Output

2014-04-12 05:41:36.433 iOS-Tutorial[1665:a0b] LocalizedFailureReason
		

Getting the Error Recovery Attempter

- recoveryAttempter

Returns an object that conforms to the NSErrorRecoveryAttempting informal protocol.

Example

NSError *error = [[NSError alloc] initWithDomain:@"com.eezytutorials.iosTuts" code:200 userInfo:@{ NSLocalizedFailureReasonErrorKey:@"LocalizedFailureReason",
        NSLocalizedDescriptionKey:@"LocalizedDescription",
        NSLocalizedRecoverySuggestionErrorKey:@"LocalizedRecoverySuggestion",
        NSLocalizedRecoveryOptionsErrorKey:@"LocalizedRecoveryOptions",
        NSRecoveryAttempterErrorKey:@"RecoveryAttempter",
        NSHelpAnchorErrorKey:@"HelpAnchor",
        NSStringEncodingErrorKey:@"NSStringEncodingError",
        NSURLErrorKey:@"NSURLError",
        NSFilePathErrorKey:@" NSFilePathError"
        }];
NSLog(@"%@",[error recoveryAttempter]);
		

Output

2014-04-12 05:41:52.581 iOS-Tutorial[1677:a0b] RecoveryAttempter
		

Displaying a Help Anchor

- helpAnchor

A string to display in response to an alert panel help anchor button being pressed.

Example

NSError *error = [[NSError alloc] initWithDomain:@"com.eezytutorials.iosTuts" code:200 userInfo:@{ NSLocalizedFailureReasonErrorKey:@"LocalizedFailureReason",
        NSLocalizedDescriptionKey:@"LocalizedDescription",
        NSLocalizedRecoverySuggestionErrorKey:@"LocalizedRecoverySuggestion",
        NSLocalizedRecoveryOptionsErrorKey:@"LocalizedRecoveryOptions",
        NSRecoveryAttempterErrorKey:@"RecoveryAttempter",
        NSHelpAnchorErrorKey:@"HelpAnchor",
        NSStringEncodingErrorKey:@"NSStringEncodingError",
        NSURLErrorKey:@"NSURLError",
        NSFilePathErrorKey:@" NSFilePathError"
        }];
NSLog(@"%@",[error helpAnchor]);
		

Output

2014-04-12 05:42:15.932 iOS-Tutorial[1687:a0b] HelpAnchor
		

Advertisements