| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/Foundation.framework |
| Availability | Available in Mac OS X v10.2 with Safari 1.0 installed. Available in Mac OS X v10.2.7 and later. Available in iPhone OS 2.0 and later. |
| Companion guide | |
| Declared in | NSError.h NSURLError.h |
| Related sample code |
An NSError object encapsulates richer and more extensible error information than is possible using only an error code or error string. The core attributes of an NSError object are an error domain (represented by a string), a domain-specific error code and a user info dictionary containing application specific information.
Several well-known domains are defined corresponding to Mach, POSIX, and OSStatus errors. Foundation error codes are found in the Cocoa error domain and documented in the Foundation Constants Reference. In addition, NSError allows you to attach an arbitrary user info dictionary to an error object, and provides the means to return a human-readable description for the error.
NSError is not an abstract class, and can be used directly. Applications may choose to create subclasses of NSError to provide better localized error strings by overriding localizedDescription.
In general, a method should signal an error condition by—for example—returning NO or nil rather than by the simple presence of an error object. The method can then optionally return an NSError object by reference, in order to further describe the error.
– localizedDescription
– localizedRecoveryOptions
– localizedRecoverySuggestion
– localizedFailureReason
Creates and initializes an NSError object for a given domain and code with a given userInfo dictionary.
+ (id)errorWithDomain:(NSString *)domain code:(NSInteger)code userInfo:(NSDictionary *)dict
The error domain—this can be one of the predefined NSError domains, or an arbitrary string describing a custom domain. domain must not be nil.
The error code for the error.
The userInfo dictionary for the error. userInfo may be nil.
An NSError object for domain with the specified error code and the dictionary of arbitrary data userInfo.
NSError.hReturns the receiver’s error code.
- (NSInteger)code
The receiver’s error code.
Note that errors are domain specific.
NSError.hReturns the receiver’s error domain.
- (NSString *)domain
A string containing the receiver’s error domain.
NSError.hReturns an NSError object initialized for a given domain and code with a given userInfo dictionary.
- (id)initWithDomain:(NSString *)domain code:(NSInteger)code userInfo:(NSDictionary *)dict
The error domain—this can be one of the predefined NSError domains, or an arbitrary string describing a custom domain. domain must not be nil.
The error code for the error.
The userInfo dictionary for the error. userInfo may be nil.
An NSError object initialized for domain with the specified error code and the dictionary of arbitrary data userInfo.
This is the designated initializer for NSError.
NSError.hReturns a string containing the localized description of the error.
- (NSString *)localizedDescription
A string containing the localized description of the error.
By default this method returns the object in the user info dictionary for the key NSLocalizedDescriptionKey. If the user info dictionary doesn’t contain a value for NSLocalizedDescriptionKey, a default string is constructed from the domain and code.
This method can be overridden by subclasses to present customized error strings.
NSError.hReturns a string containing the localized explanation of the reason for the error.
- (NSString *)localizedFailureReason
A string containing the localized explanation of the reason for the error. By default this method returns the object in the user info dictionary for the key NSLocalizedFailureReasonErrorKey.
This method can be overridden by subclasses to present customized error strings.
NSError.hReturns an array containing the localized titles of buttons appropriate for displaying in an alert panel.
- (NSArray *)localizedRecoveryOptions
An array containing the localized titles of buttons appropriate for displaying in an alert panel. By default this method returns the object in the user info dictionary for the key NSLocalizedRecoveryOptionsErrorKey. If the user info dictionary doesn’t contain a value for NSLocalizedRecoveryOptionsErrorKey, nil is returned.
The first string is the title of the right-most and default button, the second the one to the left of that, and so on. The recovery options should be appropriate for the recovery suggestion returned by localizedRecoverySuggestion. If the user info dictionary doesn’t contain a value for NSLocalizedRecoveryOptionsErrorKey, only an OK button is displayed.
This method can be overridden by subclasses to present customized recovery suggestion strings.
NSError.hReturns a string containing the localized recovery suggestion for the error.
- (NSString *)localizedRecoverySuggestion
A string containing the localized recovery suggestion for the error. By default this method returns the object in the user info dictionary for the key NSLocalizedRecoverySuggestionErrorKey. If the user info dictionary doesn’t contain a value for NSLocalizedRecoverySuggestionErrorKey, nil is returned.
The returned string is suitable for displaying as the secondary message in an alert panel.
This method can be overridden by subclasses to present customized recovery suggestion strings.
NSError.hReturns an object that conforms to the NSErrorRecoveryAttempting informal protocol.
- (id)recoveryAttempter
An object that conforms to the NSErrorRecoveryAttempting informal protocol. By default this method returns the object for the user info dictionary for the key NSRecoveryAttempterErrorKey. If the user info dictionary doesn’t contain a value for NSRecoveryAttempterErrorKey, nil is returned.
The recovery attempter must be an object that can correctly interpret an index into the array returned by localizedRecoveryOptions.
NSError.hReturns the receiver's user info dictionary.
- (NSDictionary *)userInfo
The receiver's user info dictionary, or nil if the user info dictionary has not been set.
NSError.hThese keys may exist in the user info dictionary.
extern NSString *NSLocalizedDescriptionKey; extern NSString *NSErrorFailingURLStringKey; const NSString *NSFilePathErrorKey; const NSString *NSStringEncodingErrorKey; const NSString *NSUnderlyingErrorKey; const NSString *NSURLErrorKey; const NSString *NSLocalizedFailureReasonErrorKey; const NSString *NSLocalizedRecoverySuggestionErrorKey; const NSString *NSLocalizedRecoveryOptionsErrorKey; const NSString *NSRecoveryAttempterErrorKey;
NSLocalizedDescriptionKeyThe corresponding value is a localized string representation of the error that, if present, will be returned by localizedDescription.
Available in Mac OS X v10.2 and later.
Declared in NSError.h.
NSErrorFailingURLStringKeyThe corresponding value is the URL that caused the error. This key is only present in the NSURLErrorDomain.
Available in Mac OS X v10.2 with Safari 1.0 installed.
Available in Mac OS X v10.2.7 and later.
Available in iPhone OS 2.0 and later.
Declared in NSURLError.h.
NSFilePathErrorKeyContains the file path of the error.
The corresponding value is an NSString object.
Available in Mac OS X v10.4 and later.
Declared in NSError.h.
NSStringEncodingErrorKeyThe corresponding value is an NSNumber object containing the NSStringEncoding value.
Available in Mac OS X v10.4 and later.
Declared in NSError.h.
NSUnderlyingErrorKeyThe corresponding value is an error that was encountered in an underlying implementation and caused the error that the receiver represents to occur.
Available in Mac OS X v10.3 and later.
Declared in NSError.h.
NSURLErrorKeyThe corresponding value is an NSURL object.
Available in Mac OS X v10.4 and later.
Declared in NSError.h.
NSLocalizedFailureReasonErrorKeyThe corresponding value is a localized string representation containing the reason for the failure that, if present, will be returned by localizedFailureReason.
This string provides a more detailed explanation of the error than the description.
Available in Mac OS X v10.4 and later.
Declared in NSError.h.
NSLocalizedRecoverySuggestionErrorKeyThe corresponding value is a string containing the localized recovery suggestion for the error.
This string is suitable for displaying as the secondary message in an alert panel.
Available in Mac OS X v10.4 and later.
Declared in NSError.h.
NSLocalizedRecoveryOptionsErrorKeyThe corresponding value is an array containing the localized titles of buttons appropriate for displaying in an alert panel.
The first string is the title of the right-most and default button, the second the one to the left, and so on. The recovery options should be appropriate for the recovery suggestion returned by localizedRecoverySuggestion.
Available in Mac OS X v10.4 and later.
Declared in NSError.h.
NSRecoveryAttempterErrorKeyThe corresponding value is an object that conforms to the NSErrorRecoveryAttempting informal protocol.
The recovery attempter must be an object that can correctly interpret an index into the array returned by recoveryAttempter.
Available in Mac OS X v10.4 and later.
Declared in NSError.h.
NSError.hThe following error domains are predefined.
const NSString *NSPOSIXErrorDomain; const NSString *NSOSStatusErrorDomain; const NSString *NSMachErrorDomain;
NSPOSIXErrorDomainPOSIX/BSD errors
Available in Mac OS X v10.2 and later.
Declared in NSError.h.
NSOSStatusErrorDomainMac OS 9/Carbon errors
Available in Mac OS X v10.2 and later.
Declared in NSError.h.
NSMachErrorDomainMach errors
Available in Mac OS X v10.2 and later.
Declared in NSError.h.
Additionally, the following error domain is defined by Core Foundation:
CFStreamErrorDomain | Defines constants for values returned in the domain field of the CFStreamError structure. |
NSError.h
© 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-05-06)