美文网首页
iOS NSError

iOS NSError

作者: gaookey | 来源:发表于2020-06-08 16:21 被阅读0次
    NSError *error = [[NSError alloc] initWithDomain:@"testDomain"
                                                code:9998
                                            userInfo:@{NSLocalizedDescriptionKey:@"错误描述",
                                                       NSLocalizedFailureReasonErrorKey:@"错误原因",
                                                       NSLocalizedRecoverySuggestionErrorKey:@"解决建议",
                                                       NSLocalizedRecoveryOptionsErrorKey:@[@"解决建议可选1",@"解决建议可选2"]}];
    
    // Domain 错误域:产生错误的地方(系统提供了一些定义的域)
    /*
     FOUNDATION_EXPORT NSString *const NSCocoaErrorDomain;
     FOUNDATION_EXPORT NSString *const NSPOSIXErrorDomain;
     FOUNDATION_EXPORT NSString *const NSOSStatusErrorDomain;
     FOUNDATION_EXPORT NSString *const NSMachErrorDomain;
     */
    NSString *domain = error.domain;
    
    // code:错误码
    NSInteger code = error.code;
    
    // userInfo:错误信息(系统提供了很多定义的key)
    NSDictionary *userInfo = error.userInfo;
    
    NSString *localizedDescription = userInfo[NSLocalizedDescriptionKey];
    NSString *localizedFailureReason = userInfo[NSLocalizedFailureReasonErrorKey];
    NSString *localizedRecoverySuggestion = userInfo[NSLocalizedRecoverySuggestionErrorKey];
    NSArray *localizedRecoveryOptions = userInfo[NSLocalizedRecoveryOptionsErrorKey];
    

    相关文章

      网友评论

          本文标题:iOS NSError

          本文链接:https://www.haomeiwen.com/subject/tlmrtktx.html