zoukankan      html  css  js  c++  java
  • IOS开发之----NSError错误信息

    2015年01月12日 14:51:10

    一、获取系统的错误信息
    比如移动文件时,获取文件操作错误:
    1.  
      NSError *e = nil;
    2.  
      [[NSFileManager defaultManager] moveItemAtPath:sourcePath toPath:targetPath error:&e];
    3.  
      if (e) {
    4.  
        NSLog(@"move failed:%@", [e localizedDescription]);
    5.  
      }
    先定一个空的错误信息
    NSError *e = nil;
    取地址
    &e
    如果有错误信息,打印错误的本地化描述
    1.  
      if (e) {
    2.  
        NSLog(@"move failed:%@", [e localizedDescription]);
    3.  
      }
    二、自定义错误信息
    通常可以通过下面语句,自定义个NSError对象
    1.  
      #define CustomErrorDomain @"com.xiaodao.test"
    2.  
      typedef enum {
    3.  
         XDefultFailed = -1000,
    4.  
         XRegisterFailed,
    5.  
          XConnectFailed,
    6.  
         XNotBindedFailed
    7.  
      }CustomErrorFailed;
    8.  
      NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"is a error test"
    9.  
      forKey:NSLocalizedDescriptionKey];
    10.  
      NSError *aError = [NSError errorWithDomain:CustomErrorDomain code:XDefultFailed userInfo:userInfo];
    其中,自定义错误域对象CustomErrorDomain,通常用域名反写,也可以是任何其他字符串
    code错误标识, 系统的code一般都大于零,自定code可以用枚举(最好用负数, 但不是必须的)
    userInfo自定义错误信息,NSLocalizedDescriptionKey是NSError头文件中预定义的键,标识错误的本地化描述
    可以通过NSError的localizedDescription方法获得对应的值信息


    主调用函数一般传入NSError指针的指针,来获取错误信息,例如
    1.  
      - (Bool)doSomething:(NSDictionary *)parameter1 error:(NSError **)aError
    2.  
      {
    3.  
      //TODO: do something
    4.  
        *aError = [NSError errorWithDomain:CustomErrorDomain code:XDefultFailed userInfo:userInfo];
    5.  
        return Yes;
    6.  
      }
    三、NSError头文件解析
    1.NSError对象中,主要有三个私有变量
    错误域(NSInteger): _domain
    错误标示(NSString *):_code
    错误详细信息(NSDictionary *):_userInfo
    通常用_domain和_code一起标识一个错误信息


    获取_domain
    - (NSString *)domain;
    获取 _code


    - (NSInteger)code;
    获取 _userInfo


    - (NSDictionary *)userInfo;
     
    2.预定义域


    AppKit和Foundation库中主要的错误域


    NSString *const NSCocoaErrorDomain;
    其他域
    1.  
      NSString *const NSPOSIXErrorDomain;
    2.  
      NSString *const NSOSStatusErrorDomain;
    3.  
      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;
    其他键
    1.  
      NSString *const NSRecoveryAttempterErrorKey; 
    2.  
      NSString *const NSHelpAnchorErrorKey;
    3.  
      NSString *const NSStringEncodingErrorKey ;
    4.  
      NSString *const NSURLErrorKey;
    5.  
      NSString *const NSFilePathErrorKey;
    用法:
    NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"这是错误详细的描述信息", NSLocalizedDescriptionKey, error, NSUnderlyingErrorKey, nil]];

    4.主要的初始化方法:

    1.  
      - (id)initWithDomain:(NSString *)domain code:(NSInteger)code userInfo:(NSDictionary *)dict;
    2.  
      + (id)errorWithDomain:(NSString *)domain code:(NSInteger)code userInfo:(NSDictionary *)dict
  • 相关阅读:
    Java 8 Lambda 表达式
    OSGi 系列(十二)之 Http Service
    OSGi 系列(十三)之 Configuration Admin Service
    OSGi 系列(十四)之 Event Admin Service
    OSGi 系列(十六)之 JDBC Service
    OSGi 系列(十)之 Blueprint
    OSGi 系列(七)之服务的监听、跟踪、声明等
    OSGi 系列(六)之服务的使用
    OSGi 系列(三)之 bundle 事件监听
    OSGi 系列(三)之 bundle 详解
  • 原文地址:https://www.cnblogs.com/sundaysgarden/p/13793111.html
Copyright © 2011-2022 走看看