NSPredicate谓词-数组过滤

NSPredicate谓词-数组过滤

NSPredicate中主要的几种运算方式

1、比较运算符: > 、< 、== 、 >= 、<= 、 !=
数值
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"score >= 80"];
字符串
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"certType == \'NI\'"];
certType:是自定义类的属性或者是字典的key,字符串NI要用单引号
2、逻辑运算符:and、or、not
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"type >= 3 and score >= 80"];
3、范围运算符:in、between
4、字符串本身:self
5、字符串相关:beginswith、endswith、contains
NSString *str = @"good";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains[cd] %@",str];

注:[c]不区分大小写 , [d]不区分发音符号即没有重音符号 , [cd]既不区分大小写,也不区分发音符号。

6、通配符:like
NSString *str = @"good*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self like[cd] %@",str];
7、正则表达式:matches

例:NSString regex = @"good";

//数字、字母、-组成,5-10位
NSString * regex = @"^[0-9A-Za-z-]{5,10}$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL result = [pred evaluateWithObject:self.passenger.rPostCode];
正确为yes

对对象集合的筛选

NSString *name = @"name";
NSString *str = @"005";
NSPredicate *predicate0 = [NSPredicate predicateWithFormat:@"%@ == %@",name,str];
//po predicate1.predicateFormat:"name" == "005",零个结果,语法不对
NSArray *predicateArray0 = [array filteredArrayUsingPredicate:predicate0];

NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"self.%@ == %@",name,str];
//po predicate1.predicateFormat:SELF."name" == "005",1个结果,语法正确
NSArray *predicateArray1 = [array filteredArrayUsingPredicate:predicate1];

NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"name == %@",str];
//po predicate2.predicateFormat: name == "005",有一个结果,语法正确
NSArray *predicateArray2 = [array filteredArrayUsingPredicate:predicate2];

NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"name == \'005\'"];
//po predicate3.predicateFormat: name == "005",有一个结果,语法z'q
NSArray *predicateArray3 = [array filteredArrayUsingPredicate:predicate3];

附上源码:

//
//  GGPredicateViewController.m
//  testDemo
//
//  Created by lignpeng on 2017/7/24.
//  Copyright © 2017年 genpeng. All rights reserved.
//

#import "GGPredicateViewController.h"

@interface User : NSObject

@property(nonatomic, strong) NSString *name;
@property(nonatomic, assign) NSInteger type;
@property(nonatomic, assign) NSInteger score;

@end

@implementation User

+ (instancetype)user:(NSString *)name type:(NSInteger )type {
    User *user = [User new];
    user.name = name;
    user.type = type;
    user.score = arc4random()%100;
    return user;
}

@end

@interface GGPredicateViewController ()

@end

@implementation GGPredicateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

- (void)initView {
    self.view.backgroundColor = [UIColor whiteColor];
    CGFloat margin = 32;
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(margin, margin * 3, CGRectGetWidth([UIScreen mainScreen].bounds) - margin * 2, 42)];
    [button addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"action" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor blueColor];
    button.layer.cornerRadius = 5;
    button.clipsToBounds = YES;
    [self.view addSubview:button];
}

- (void)action {
    [self objects];
    [self strings];
}

- (void)objects{
    NSMutableArray *array = [NSMutableArray array];
    NSUInteger index = 6;
    while (index > 0) {
        [array addObject:[User user:[NSString stringWithFormat:@"00%lu",(unsigned long)index] type:index]];
        index--;
    }
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"type >= 3 or score >= 80"];
    //po predicate.predicateFormat: type >= 3 OR score >= 80
    NSArray *predicateArray = [array filteredArrayUsingPredicate:predicate];
    NSString *name = @"name";
    NSString *str = @"005";
    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"%@ == %@",name,str];
    //po predicate1.predicateFormat:"name" == "005",零个结果
    NSArray *predicateArray1 = [array filteredArrayUsingPredicate:predicate1];
    
    NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"name == %@",str];
    //po predicate2.predicateFormat: name == "005",有一个结果
    NSArray *predicateArray2 = [array filteredArrayUsingPredicate:predicate2];
}

- (void)strings {
    NSArray *array = @[@"willback",@"backhome",@"goodmonring",@"gooDbyd"];
    NSString *str = @"good";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains[c] %@",str];
    //有两个结果
    NSArray *predicateArray = [array filteredArrayUsingPredicate:predicate];
    
    str = @"gooDbyd";
    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"self == %@",str];
    //po predicate1 SELF == "gooDbyd",有一个结果
    NSArray *predicateArray1 = [array filteredArrayUsingPredicate:predicate1];
}

@end

你可能感兴趣的:(NSPredicate谓词-数组过滤)