First, get the error message of the system
For example, when moving files, get file operation error:
Nserror *e = nil;
[[Nsfilemanager Defaultmanager] Moveitematpath:sourcepath topath:targetpath error:&e];
if (e) {
NSLog (@ "Move failed:%@", [e Localizeddescription]);
}
To set an empty error message first
Nserror *e = nil;
Fetch Address
&e
If there is an error message, print the localized description of the error
if (e) {NSLog (@ "Move failed:%@", [e Localizeddescription]);}
Ii.. Custom error messages
You can usually customize a Nserror object by using the following statement
#define Customerrordomain @ "com.xiaodao.test" typedef enum { xdefultfailed = -1000, xregisterfailed,
xconnectfailed, xnotbindedfailed }customerrorfailed;
Nsdictionary *userinfo = [Nsdictionary dictionarywithobject:@ "is a error test" Forkey:nslocalizeddescriptionkey];
Nserror *aerror = [Nserror errorwithdomain:customerrordomain code:xdefultfailed Userinfo:userinfo];
Where the custom error domain object is customerrordomain, usually with the domain name reversed, or any other string
Code error ID, the system code is generally greater than 0, custom code can be enumerated (preferably with negative numbers, but not necessary)
UserInfo custom error message, Nslocalizeddescriptionkey is a pre-defined key in the Nserror header file that identifies the localized description of the error
The corresponding value information can be obtained by Nserror's Localizeddescription method.
The main calling function typically passes a pointer to the Nserror pointer to get an error message, such as
-(Bool) dosomething: (nsdictionary *) Parameter1 Error: (Nserror *) aerror{ //todo:do Something
*aerror = [Nserror errorwithdomain:customerrordomain code:xdefultfailed Userinfo:userinfo];
return Yes;
}
Third, Nserror header file parsing
There are three main private variables in a 1.NSError object
Error domain (Nsinteger): _domain
Error indication (nsstring *): _code
Error details (nsdictionary *): _userinfo
Usually identify an error message together with _domain and _code
Get _domain
-(NSString *) domain;
Get _code
-(Nsinteger) code;
Get _userinfo
-(Nsdictionary *) UserInfo;
2. Predefined domains
Major error domains in the AppKit and foundation libraries
NSString *const Nscocoaerrordomain;
Other domains
NSString *const Nsposixerrordomain; NSString *const Nsosstatuserrordomain; NSString *const Nsmacherrordomain;
3. Pre-defined UserInfo key name
Recommended standard method, universal key
NSString *const Nsunderlyingerrorkey;
Other keys, corresponding to the method of reading information:
Detailed Description key
NSString *const Nslocalizeddescriptionkey;
Fetch method
-(NSString *) localizeddescription;
Failure reason Key
NSString *const Nslocalizedfailurereasonerrorkey
Fetch method
-(NSString *) Localizedfailurereason;
Restore Suggestion Key
NSString *const Nslocalizedrecoverysuggestionerrorkey;
Fetch method
-(NSString *) localizedrecoverysuggestion;
Recovery Option key
NSString *const Nslocalizedrecoveryoptionserrorkey
Fetch method
-(Nsarray *) localizedrecoveryoptions;
Other keys
NSString *const Nsrecoveryattemptererrorkey; NSString *const Nshelpanchorerrorkey; NSString *const Nsstringencodingerrorkey; NSString *const Nsurlerrorkey; NSString *const Nsfilepatherrorkey;
Usage:
Nsdictionary *userinfo = [Nsdictionary dictionarywithobjectsandkeys:@ "This is a detailed description of the error", Nslocalizeddescriptionkey, error , Nsunderlyingerrorkey, Nil]];
4. The main initialization method:
-(ID) Initwithdomain: (NSString *) Domain code: (nsinteger) code userInfo: (nsdictionary *) dict;
+ (ID) Errorwithdomain: (NSString *) Domain code: (nsinteger) code userInfo: (nsdictionary *) dict;
Error message Nserror