iOS--NSError的自定义以及使用

NSError的应用在实际开发中也是经常会接触到的,这是我在做登录界面的时候,我希望输入框输入完成后即刻判断输入是否正确,如果输入错误需要显示错误信息。
也想过用一个block里边装个数组@[BOOL,NSString]这样来完成需求,其实也是可以的。不过何不用用NSError呢?

下边我贴出了我定义的CheckManger(信息检测类):

在manager的头文件中

#import <Foundation/Foundation.h>

typedef void(^completed)(BOOL isCorrect, NSError *error);

@interface LLCheckManager : NSObject

/**验证手机号码是否正确*/
+ (void)checkPhoneNumber:(NSString *)phoneNumString completed:(completed) completed;

/**验证邮箱是否输入正确*/
+ (void)checkEmail:(NSString *)emailString completed:(completed)completed;

/**验证密码是否正确*/
+ (void)checkPassworld:(NSString *)passwordString completed:(completed)completed;

@end

在manager的m文件中:
首先需要知道NSError的构建参数,
1.domain:—— 错误域 (自定义,作用我不是很清楚暂且理解为提示作用吧)
2.code: —— 错误编码(自己定义,编码应当与错误内容匹配,相当于看到404就知道是访问不了)
3.userInfo:——错误原因(自己定义,给出错误的原因)

具体信息且看代码:

#import "LLCheckManager.h"

#define LLErrorDomain @" An Error Has Occurred"

typedef enum {
    LLErrorDefult = 0,
    LLErrorConnect   ,
    LLErrorNotFind,
    LLErrorMatch
}LLErrorFailed;

@implementation LLCheckManager

#pragma mark —— 定义错误
/**返回一个错误*/
+ (NSError *)errorWithCode:(LLErrorFailed)code{
    NSString * localizedDescription;
    switch (code) {
        case LLErrorConnect:
            localizedDescription = @"网络链接错误";
            break;
        case LLErrorNotFind:
            localizedDescription = @"没有找到";
            break;
            case LLErrorMatch:
            localizedDescription = @"匹配错误";
            break;
        default:
            localizedDescription = @"输入错误";
            break;
    }

    NSDictionary * userInfo = [NSDictionary dictionaryWithObject:localizedDescription forKey:NSLocalizedDescriptionKey];
    NSError * aError = [NSError errorWithDomain:LLErrorDomain code:code userInfo:userInfo];
    return aError;
}

#pragma mark —— 验证

/**验证手机号码*/
+ (void)checkPhoneNumber:(NSString *)phoneNumString completed:(completed)completed{

    NSString *phoneNumFormat = @"^1[2|3|4|5|6|7|8][0-9]{9}$";
    //表示字符串以1开头,紧接着是2、3、4、5、6、7、8中的任意一个,然后是0~9中的数字 以9个 结尾。

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneNumFormat];
    BOOL isMatch = [pred evaluateWithObject:phoneNumString];
    completed(isMatch,[self errorWithCode:LLErrorDefult]);
}

//验证邮箱
+ (void)checkEmail:(NSString *)emailString completed:(completed)completed{
    NSString * emailFormat = @"^(([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]";
    NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailFormat];
    BOOL isMatch = [pred evaluateWithObject:emailString];
    completed(isMatch,[self errorWithCode:LLErrorDefult]);
}

/**验证密码*/
+ (void)checkPassworld:(NSString *)passwordString completed:(completed)completed{
    NSString *passwordFormat = @"^[A-Za-z0-9]{6,20}+$";
    //表示字符串以大写字母 或 小写字母 或 数字 中的6到20个 组成
    NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCH %@",passwordFormat];
    BOOL isMatch = [pred evaluateWithObject:passwordString];
    completed(isMatch,[self errorWithCode:LLErrorDefult]);  
}

@end

在ViewController中如何用到error呢?

直接调用方法,和一般的error用法相同

#define mark —— textField Delegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
    [LLCheckManager checkPhoneNumber:textField.text completed:^(BOOL isCorrect, NSError *error) {
        if (isCorrect) {
            NSLog(@"账号输入正确");
        }else{
           // NSLog(@"%@",error);
          NSLog(@"%@",error.localizedDescription);
        }
    }];
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值