iOS NSError详解 NSError错误code对照表 自定义、定制NSError 一看就懂
一、nserror是错误信息类
//两种初始化方法:其中,domain 不能为空 dict可以为空
- (instancetype)initwithdomain:(nserrordomain)domain code:(nsinteger)code userinfo:(nullable nsdictionary *)dict;
+ (instancetype)errorwithdomain:(nserrordomain)domain code:(nsinteger)code userinfo:(nullable nsdictionary *)dict;
@property (readonly, copy) nserrordomain domain;//错误域
错误域主要有四个,对于carbon框架的error,归于osstatus domain(nsosstatuserrordomain),对于posix error,归于nsposixerrordomain,而对于我们的ios开发,一般使用nscocoaerrordomain。nserror.h定义了四个domain
{
// 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;
}
@property (readonly) nsinteger code;//错误代码
预定义的userinfo键名 nsstring *const nsunderlyingerrorkey;//推荐的标准方式,通用键 //其他键 nsstring *const nsrecoveryattemptererrorkey; 用法示例: nsdictionary *userinfo1 = [nsdictionary dictionarywithobjectsandkeys:@"由于文件不存在,无法打开", nslocalizeddescriptionkey, @"失败原因:文件不存在", nslocalizedfailurereasonerrorkey, @"恢复建议:请创建该文件",nslocalizedrecoverysuggestionerrorkey,nil]; 二、nserror错误code对照表 foundation constants reference
三、自定义nserror 系统的nserror是可以自己定制的,以下提供代码来实现并表示如何使用: yxerror.h 与yxerror.m 以下是使用的代码:
@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
@property (nullable, readonly, strong) id recoveryattempter;
@property (nullable, readonly, copy) nsstring *helpanchor;
nsstring *const nslocalizeddescriptionkey; //
详细描述键
nsstring *const nslocalizedfailurereasonerrorkey; // 失败原因键
nsstring *const nslocalizedrecoverysuggestionerrorkey; //恢复建议键
nsstring *const nslocalizedrecoveryoptionserrorkey; // 恢复选项键
nsstring *const nshelpanchorerrorkey;
nsstring *const nsstringencodingerrorkey ;
nsstring *const nsurlerrorkey;
nsstring *const nsfilepatherrorkey;
nserror *error = [[nserror alloc] initwithdomain:nscocoaerrordomain code:4 userinfo:userinfo1];//此处code是4,对照下面对照表4代表文件不存在。userinfo传userinfo1 查看自定义打印。userinfo传nil,查看本地化描述。
nslog(@"========%@",[error localizeddescription]);
nslog(@"========%@",[error localizedfailurereason]);
nslog(@"========%@",[error localizedrecoverysuggestion]);
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
}//
// yxerror.h
// customyxerror
//
// copyright (c) 2014年 y.x. all rights reserved.
//
#import
// 自行添加错误码
typedef enum : nsuinteger {
crash = -10000,
disconnect,
unknow,
} yxerrorcode;
@interface yxerror : nsobject
+ (nserror *)errorcode:(yxerrorcode)code userinfo:(nsdictionary *)dic;
+ (nsstring *)transformcodetostringinfo:(yxerrorcode)code;
@end
//
// yxerror.m
// customyxerror
//
// copyright (c) 2014年 y.x. all rights reserved.
//
#import "yxerror.h"
static nsdictionary *errordictionary = nil;
@implementation yxerror
+ (void)initialize
{
if (self == [yxerror class])
{
errordictionary = \
@{
/* code : errorwithdomain */
/* ==================================== */
@(crash) : @"crash",
@(disconnect) : @"disconnect",
@(unknow) : @"unknow",
/* ==================================== */
};
}
}
+ (nserror *)errorcode:(yxerrorcode)code userinfo:(nsdictionary *)dic
{
return [nserror errorwithdomain:errordictionary[@(code)]
code:code
userinfo:dic];
}
+ (nsstring *)transformcodetostringinfo:(yxerrorcode)code
{
return errordictionary[@(code)];
}
@end
//
// appdelegate.m
// nserror
//
// copyright (c) 2014年 youxianming. all rights reserved.
//
#import "appdelegate.h"
#import "yxerror.h"
@implementation appdelegate
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
{
// 定制一个nserror
nserror *error = [yxerror errorcode:crash
userinfo:nil];
nslog(@"%@", error);
// 翻译一个nserror(将code值翻译成字符串)
nslog(@"%@", [yxerror transformcodetostringinfo:-10000]);
return yes;
}
@end