NSError类

一、NSError 是系统错误信息类

1. 两种初始化方法:其中,dimain(域)不能为空,dict可以为空
//实例方法
- (instancetype)initWithDomain:(NSErrorDomain)domain code:(NSInteger)code userInfo:(nullable NSDictionary *)dict;
//类方法初始化
+ (instancetype)errorWithDomain:(NSErrorDomain)domain code:(NSInteger)code userInfo:(nullable NSDictionary *)dict;

2. 错误域(第一个参数)
//属性
@property (readonly, copy) NSErrorDomain domain;//错误域

@property (readonly) NSInteger code;//错误代码

@property (readonly, copy) NSDictionary *userInfo;//错误信息


@property (readonly, copy) NSString *localizedDescription;//获取本地化描述


@property (nullable, readonly, copy) NSString *localizedFailureReason;//获取失败原因


@property (nullable, readonly, copy) NSString *localizedRecoverySuggestion;//获取恢复建议


@property (nullable, readonly, copy) NSArray *localizedRecoveryOptions;本地恢复建议


@property (nullable, readonly, strong) id recoveryAttempter;


@property (nullable, readonly, copy) NSString *helpAnchor;

错误域主要有四个:

  • NSCocoaErrorDomain: 针对我们iOS开发(我们主要用这个)
  • NSOSStatusErrorDomain:针对Carbon框架的Error
  • NSPOSIXErrorDomain:针对POSIX的Error

NS_ASSUME_NONNULL_BEGIN

// Predefined domain for errors from most AppKit and Foundation APIs.
FOUNDATION_EXPORT NSErrorDomain const NSCocoaErrorDomain;

// Other predefined domains; value of "code" will correspond to preexisting values in these domains.
FOUNDATION_EXPORT NSErrorDomain const NSPOSIXErrorDomain;
FOUNDATION_EXPORT NSErrorDomain const NSOSStatusErrorDomain;
FOUNDATION_EXPORT NSErrorDomain const NSMachErrorDomain;

4. userinfo(第三个参数)

第三个参数是一个字典,其中预定义的userinfo键名如下

//常用键
 NSString *const NSUnderlyingErrorKey;//推荐的标准方式,通用键
 NSString *const NSLocalizedDescriptionKey;             // 详细描述键
 NSString *const NSLocalizedFailureReasonErrorKey;      // 失败原因键
 NSString *const NSLocalizedRecoverySuggestionErrorKey; //恢复建议键
 NSString *const NSLocalizedRecoveryOptionsErrorKey;    // 恢复选项键

//其他键
 NSString *const NSRecoveryAttempterErrorKey;    
 NSString *const NSHelpAnchorErrorKey;              
 NSString *const NSStringEncodingErrorKey ;  
 NSString *const NSURLErrorKey;       
 NSString *const NSFilePathErrorKey;      

//用法示例:
 NSDictionary *userInfo = @{
                               NSLocalizedDescriptionKey:@"由于文件不存在,无法打开",
                               NSLocalizedFailureReasonErrorKey:@"失败原因:文件不存在",
                               NSLocalizedRecoverySuggestionErrorKey:@"恢复建议:请创建该文件"
                               };
 //此处code是4,对照下面对照表4代表文件不存在。userinfo传userinfo1 查看自定义打印。userinfo传nil,查看本地化描述。
 NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:4 userInfo:userInfo1];   
 NSLog(@"========%@",[error localizedDescription]);
 NSLog(@"========%@",[error localizedFailureReason]);
 NSLog(@"========%@",[error localizedRecoverySuggestion]);
打印结果:
2019-02-28 11:50:40.469721+0800 emoji maker[57155:1624717] ========由于文件不存在,无法打开
2019-02-28 11:50:40.469816+0800 emoji maker[57155:1624717] ========失败原因:文件不存在
2019-02-28 11:50:40.469881+0800 emoji maker[57155:1624717] ========恢复建议:请创建该文件
3. 错误代码(第二个参数附:code对照表)
Foundation Constants Reference
NSError Codes

NSError codes in the Cocoa error domain.

enum {
    NSFileNoSuchFileError = 4,
    NSFileLockingError = 255,
    NSFileReadUnknownError = 256,
    NSFileReadNoPermissionError = 257,
    NSFileReadInvalidFileNameError = 258,
    NSFileReadCorruptFileError = 259,
    NSFileReadNoSuchFileError = 260,
    NSFileReadInapplicableStringEncodingError = 261,
    NSFileReadUnsupportedSchemeError = 262,
    NSFileReadTooLargeError = 263,
    NSFileReadUnknownStringEncodingError = 264,
    NSFileWriteUnknownError = 512,
    NSFileWriteNoPermissionError = 513,
    NSFileWriteInvalidFileNameError = 514,
    NSFileWriteInapplicableStringEncodingError = 517,
    NSFileWriteUnsupportedSchemeError = 518,
    NSFileWriteOutOfSpaceError = 640,
    NSFileWriteVolumeReadOnlyError = 642m
    NSKeyValueValidationError = 1024,
    NSFormattingError = 2048,
    NSUserCancelledError = 3072,
    
    NSFileErrorMinimum = 0,
    NSFileErrorMaximum = 1023,
    NSValidationErrorMinimum = 1024,
    NSValidationErrorMaximum = 2047,
    NSFormattingErrorMinimum = 2048,
    NSFormattingErrorMaximum = 2559,
    
    NSPropertyListReadCorruptError = 3840,
    NSPropertyListReadUnknownVersionError = 3841,
    NSPropertyListReadStreamError = 3842,
    NSPropertyListWriteStreamError = 3851,
    NSPropertyListErrorMinimum = 3840,
    NSPropertyListErrorMaximum = 4095
    
    NSExecutableErrorMinimum = 3584,
    NSExecutableNotLoadableError = 3584,
    NSExecutableArchitectureMismatchError = 3585,
    NSExecutableRuntimeMismatchError = 3586,
    NSExecutableLoadError = 3587,
    NSExecutableLinkError = 3588,
    NSExecutableErrorMaximum = 3839,
    
}
URL Loading System Error Codes

These values are returned as the error code property of an NSError object with the domain “NSURLErrorDomain”.

typedef enum
{
    NSURLErrorUnknown = -1,
    NSURLErrorCancelled = -999,
    NSURLErrorBadURL = -1000,
    NSURLErrorTimedOut = -1001,
    NSURLErrorUnsupportedURL = -1002,
    NSURLErrorCannotFindHost = -1003,
    NSURLErrorCannotConnectToHost = -1004,
    NSURLErrorDataLengthExceedsMaximum = -1103,
    NSURLErrorNetworkConnectionLost = -1005,
    NSURLErrorDNSLookupFailed = -1006,
    NSURLErrorHTTPTooManyRedirects = -1007,
    NSURLErrorResourceUnavailable = -1008,
    NSURLErrorNotConnectedToInternet = -1009,
    NSURLErrorRedirectToNonExistentLocation = -1010,
    NSURLErrorBadServerResponse = -1011,
    NSURLErrorUserCancelledAuthentication = -1012,
    NSURLErrorUserAuthenticationRequired = -1013,
    NSURLErrorZeroByteResource = -1014,
    NSURLErrorCannotDecodeRawData = -1015,
    NSURLErrorCannotDecodeContentData = -1016,
    NSURLErrorCannotParseResponse = -1017,
    NSURLErrorFileDoesNotExist = -1100,
    NSURLErrorFileIsDirectory = -1101,
    NSURLErrorNoPermissionsToReadFile = -1102,
    NSURLErrorSecureConnectionFailed = -1200,
    NSURLErrorServerCertificateHasBadDate = -1201,
    NSURLErrorServerCertificateUntrusted = -1202,
    NSURLErrorServerCertificateHasUnknownRoot = -1203,
    NSURLErrorServerCertificateNotYetValid = -1204,
    NSURLErrorClientCertificateRejected = -1205,
    NSURLErrorClientCertificateRequired = -1206,
    NSURLErrorCannotLoadFromNetwork = -2000,
    NSURLErrorCannotCreateFile = -3000,
    NSURLErrorCannotOpenFile = -3001,
    NSURLErrorCannotCloseFile = -3002,
    NSURLErrorCannotWriteToFile = -3003,
    NSURLErrorCannotRemoveFile = -3004,
    NSURLErrorCannotMoveFile = -3005,
    NSURLErrorDownloadDecodingFailedMidStream = -3006,
    NSURLErrorDownloadDecodingFailedToComplete = -3007
}

参考文章:
iOS NSError详解 NSError错误code对照表 自定义、定制NSError 一看就懂

你可能感兴趣的:(NSError类)