background image

iOS

      

错误信息

 

    NSError

 

  

一、获取系统的错误信息
比如移动文件时,获取文件操作错误:

1

NSError *e = nil;  

2

[[NSFileManager defaultManager] moveItemAtPath:sourcePath toPath:targetPath 

error:&e];  

3

if

 (e) {  

4

  

NSLog(@

"move failed:%@"

, [e localizedDescription]);  

5

}  

先定一个空的错误信息

6

NSError *e = nil;  

取地址

7

&e  

如果有错误信息,打印错误的本地化描述

8

if

 (e) {  

9

  

NSLog(@

"move failed:%@"

, [e localizedDescription]);  

10

}  

二、自定义错误信息

通常可以通过下面语句,自定义个

NSError 对象

11 #define CustomErrorDomain @"com.xiaodao.test"

  

12

typedef

 

enum

 {  

13

  

14

  

 XDefultFailed = -1000,  

15

  

16

  

 XRegisterFailed,  

17

  

18

  

 XConnectFailed,  

19

  

20

  

 XNotBindedFailed  

21

  

22

}CustomErrorFailed;