IOS NSPredicate 功能

作者: 辛小二 | 来源:发表于2017-05-04 14:27 被阅读105次
功能实现

我们常常会见到如下图展示 这样的需求,我们点击搜索按钮进行搜索。那么常见的两种方式实现这种需求,1、第一种就是走接口,然后更换数据源。2、就是利用oc自带的NSPredicate 去检索你要查询的数据,然后更换数据源。下面就说下这个自带NSPredicate实现检索的方法(第一种就不说了)


搜索

我们看到后台返回数据都是这样的

{
    memberPosition = 啧啧啧,
    plusv = 0,
    memberMobile = 18211111111,
    memberSchool = dddd,
    memberId = 645,
    memberNick = 那你呢,
    memberMajor = sss,
    companyName = 事实上,
    friendId = 640,
    memberLogo = /Public/Uploads/201704141353242348009.png,
    authStatus = 0,
    status = 2
},

最外层是数组,里面每一层都是这样的小字典,那么你要检索的可能只是companyName 上面的一个文字,或者memberSchool的一个字母“m”,所以我们需要通过检索的方式来检索你要的key下面的value 代码如下

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.memberNick contains[cd] %@",content];
    NSMutableArray * dataArray = [[self.viewModel.friendModelsArr filteredArrayUsingPredicate:predicate]mutableCopy];

content 就是你监听搜索的内容,@"self.memberNick contains[cd] %@"就代表你要查询self.memberNick这个key下面所有value,然后将获取到的数组赋值到数据源上reload 就可以实现想要的效果了。

如果大家上面的解释没看懂,大家请看下面的例子:

 NSDictionary *parma = @{
                            @"memberPosition":@"啧啧zizi",
                            @"memberSchool":@"学校名称",
                            @"memberId":@"250",
                            @"memberNick":@"昵称",
                            @"memberMajor":@"啦啦啦",
                            };
    NSDictionary *parma1 = @{
                            @"memberPosition":@"asasasa",
                            @"memberSchool":@"vcvcvcv",
                            @"memberId":@"rgrtrt",
                            @"memberNick":@"rbggnhn",
                            @"memberMajor":@"ewewewe",
                            };
    NSDictionary *parma2 = @{
                            @"memberPosition":@"mmmmm",
                            @"memberSchool":@"pppp",
                            @"memberId":@"99999",
                            @"memberNick":@"00000",
                            @"memberMajor":@"vvvvv",
                            };
    NSMutableArray * arr = [NSMutableArray arrayWithObjects:parma,parma1,parma2, nil];
    NSLog(@"%@",arr);
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.memberPosition CONTAINS[cd] 'asasasa'"];
    NSLog(@"%@",predicate);
    NSMutableArray * dataArray = [[arr filteredArrayUsingPredicate:predicate]mutableCopy];
    NSLog(@"%@",dataArray);

注:你可以看到 self.memberPosition CONTAINS[cd] 'asasasa' 的意思就是搜寻数组arr中,三个对象parma parma1 parma2中memberPosition这个key所对应的value有哪个是有asasasa的,如果有他就将整个对象都取出来,然后重新放到数组中,实现精准查询。

下面是常用的一些例子

(1)对NSArray进行过滤

NSArray *array = [[NSArray alloc]initWithObjects:@"beijing",@"shanghai",@"guangzou",@"wuhan", nil];    
NSString *string = @"ang";    
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",string];    

NSLog(@"%@",[array filteredArrayUsingPredicate:pred]);    

2)判断字符串首字母是否为字母:

NSString *regex = @"[A-Za-z]+";    
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];    
    
if ([predicate evaluateWithObject:aString]) {    
}  

3)各种验证

//1.0 正则验证(通用) regex 正则表达式 返回值:验证结果
- (BOOL)regular:(NSString *)regex {
    NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
    return [predicateRe evaluateWithObject:self];
}

//1.1 验证电话号码
- (BOOL)checkTelephoneNumber {
    NSString *regex = @"^1[3|4|5|7|8][0-9]\\d{8}$";
    NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
    return [predicateRe evaluateWithObject:self];
}

//1.2 验证身份证
- (BOOL)checkIDCard {
    NSString *regex = @"\\d{15}(\\d\\d[0-9xX])?";
    NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
    return [predicateRe evaluateWithObject:self];
}

//1.3 验证邮箱
- (BOOL)checkEmail {
    NSString *regex = @"^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
    NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
    return [predicateRe evaluateWithObject:self];
}

//1.4 验证纯数字
- (BOOL)checkJustNumber {
    NSString *regex = @"^[0-9]+$";
    NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
    return [predicateRe evaluateWithObject:self];
}

//1.5 验证URL
- (BOOL)checkURL {
    NSString *regex = @"^http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$";
    NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
    return [predicateRe evaluateWithObject:self];
}

//1.6 验证只是汉字
- (BOOL)checkJustChinese {
    NSString *regex = @"^[\u4e00-\u9fa5]+$";
    NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
    return [predicateRe evaluateWithObject:self];
}

//1.7 验证只是字母
- (BOOL)checkJustLetter {
    NSString *regex = @"^[A-Za-z]+$";
    NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
    return [predicateRe evaluateWithObject:self];
}

//1.8 验证只是小写字母
- (BOOL)checkJustLowercase {
    NSString *regex = @"^[a-z]+$";
    NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
    return [predicateRe evaluateWithObject:self];
}

//1.9 验证只是大写字母
- (BOOL)checkCapitalLetter {
    NSString *regex = @"^[A-Z]+$";
    NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
    return [predicateRe evaluateWithObject:self];
}

//1.10 验证包含特殊字符
- (BOOL)checkContainSpecialCharacter {
    NSString *regex = @"[~`!@#$%^&*':;\"\?=/<>,\\.\\{\\}\\[\\]\\(\\)]+";
    NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
    return [predicateRe evaluateWithObject:self];
}

希望能帮助到你。

本人个人微信公众号地址(喜欢记得关注😯)


辛小二个人微信公众号地址

相关文章

网友评论

    本文标题:IOS NSPredicate 功能

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