iOS 錯誤資訊 NSError

2021-06-16 22:15:20 字數 3053 閱讀 4922

比如移動檔案時,獲取檔案操作錯誤:

nserror *e = nil;

[[nsfilemanager defaultmanager] moveitematpath:sourcepath topath:targetpath error:&e];

if (e)

先定乙個空的錯誤資訊

nserror *e = nil;
取位址

&e
如果有錯誤資訊,列印錯誤的本地化描述

if (e)

通常可以通過下面語句,自定義個nserror物件

#define customerrordomain @"com.xiaodao.test"

typedef enum customerro***iled;

nsdictionary *userinfo = [nsdictionary dictionarywithobject:@"is a error test" forkey:nslocalizeddescriptionkey];

nserror *aerror = [nserror errorwithdomain:customerrordomain code:xdefultfailed userinfo:userinfo];

其中,自定義錯誤域物件customerrordomain,通常用網域名稱反寫,也可以是任何其他字串

code錯誤標識, 系統的code一般都大於零,自定code可以用列舉(最好用負數, 但不是必須的)

userinfo自定義錯誤資訊,nslocalizeddescriptionkey是nserror標頭檔案中預定義的鍵,標識錯誤的本地化描述

可以通過nserror的localizeddescription方法獲得對應的值資訊

主呼叫函式一般傳入nserror指標的指標,來獲取錯誤資訊,例如

- (bool)dosomething:(nsdictionary *)parameter1 error:(nserror **)aerror

1.nserror物件中,主要有三個私有變數

錯誤域(nsinteger): _domain

錯誤標示(nsstring *):_code

錯誤詳細資訊(nsdictionary *):_userinfo

通常用_domain和_code一起標識乙個錯誤資訊

獲取_domain

- (nsstring *)domain;
獲取 _code

- (nsinteger)code;

獲取 _userinfo

- (nsdictionary *)userinfo;

nsstring *const nscocoaerrordomain;
其他域

nsstring *const nsposixerrordomain;

nsstring *const nsosstatuserrordomain;

nsstring *const nsmacherrordomain;

3.預定義的userinfo鍵名

推薦的標準方式,通用鍵

nsstring *const nsunderlyingerrorkey;
其他鍵,對應各自讀取資訊的方法:

//詳細描述鍵

nsstring *const nslocalizeddescriptionkey;

//取方法

- (nsstring *)localizeddescription;

//失敗原因鍵

nsstring *const nslocalizedfailurereasonerrorkey

//取方法

- (nsstring *)localizedfailurereason;

//恢復建議鍵

nsstring *const nslocalizedrecoverysuggestionerrorkey;

//取方法

- (nsstring *)localizedrecoverysuggestion;

//恢復選項鍵

nsstring *const nslocalizedrecoveryoptionserrorkey

//取方法

- (nsarray *)localizedrecoveryoptions;

//其他鍵

nsstring *const nsrecoveryattemptererrorkey;

nsstring *const nshelpanchorerrorkey;

nsstring *const nsstringencodingerrorkey ;

nsstring *const nsurlerrorkey;

nsstring *const nsfilepatherrorkey;

用法:

nsdictionary *userinfo = [nsdictionary dictionarywithobjectsandkeys:@"這是錯誤詳細的描述資訊", nslocalizeddescriptionkey, error, nsunderlyingerrorkey, nil]];

4.主要的初始化方法:

- (id)initwithdomain:(nsstring *)domain code:(nsinteger)code userinfo:(nsdictionary *)dict;

+ (id)errorwithdomain:(nsstring *)domain code:(nsinteger)code userinfo:(nsdictionary *)dict;

MySQL錯誤資訊

mysql的出錯 表,根據mysql的標頭檔案mysql include mysqld error.h整理而成。錯誤之處,請大家指正。1005 建立表失敗 1006 建立資料庫失敗 1007 資料庫已存在,建立資料庫失敗 1008 資料庫不存在,刪除資料庫失敗 1009 不能刪除資料庫檔案導致刪除資...

反饋錯誤資訊

即當出現錯誤 時,自動彈出我們預先設計好的反饋視窗,目的是使用者只需簡 單地按一下滑鼠就可以把反饋資訊發到你指定的信箱,當然反饋視窗的內容我們 可以任意修改設計。如下 script function errorinfo errorwin window.open width 20,height 120...

錯誤資訊介面

三 錯誤資訊介面 既然 com 是靠各種各樣的介面來提供服務的,於是很自然地就會想到,是否有乙個介面能夠提供更豐富的錯誤資訊報告那?答案是 isupporterrorinfo。下面這段 是使用 isupporterrorinfo 的一般方法 stdmethodimp c fun 上面是原理性 在我們...