IOS谓词NSPredicate

Cocoa 提供了NSPredicate 用于指定过滤条件,谓词是指在计算机中表示计算真假值的函数

新建一个Person类,增添属性

#import <Foundation/Foundation.h>

@interface C_Person : NSObject
@property(nonatomic,assign) int pid;
@property(nonatomic,strong) NSString* name;
@property(nonatomic,assign) float height;
@end

在一个引用Person的类的.M中

#import "C_ViewController.h"
#import "C_Person.h"
@interface C_ViewController ()

@end

@implementation C_ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSMutableArray* arr=[NSMutableArray arrayWithCapacity:20];
    
    C_Person* person1=[[C_Person alloc]init];
    person1.pid=1;
    person1.name=@"name1";
    person1.height=168;
    [arr addObject:person1];
    
    C_Person* person2=[[C_Person alloc]init];
    person2.pid=2;
    person2.name=@"name2";
    person2.height=178;
    [arr addObject:person2];
    
    C_Person* person3=[[C_Person alloc]init];
    person3.pid=3;
    person3.name=@"name3";
    person3.height=188;
    [arr addObject:person3];
    
    //逻辑运算符AND,OR,NOT
    NSPredicate* pre1=[NSPredicate predicateWithFormat:@"pid>1 and height<188"];
    NSPredicate* pre2=[NSPredicate predicateWithFormat:@"pid>2 OR height<178"];
    NSPredicate* pre3=[NSPredicate predicateWithFormat:@"NOT pid>2"];
    //范围运算符 BETWEEM,IN
    //Betweem{A,B},包含A,B;
    NSPredicate* pre4=[NSPredicate predicateWithFormat:@"pid BETWEEN{2,5}"];
    NSPredicate* pre5=[NSPredicate predicateWithFormat:@"name IN{'name1','name2'}"];
    
    int i=0;
    for(;i<[arr count];i++)
    {
        C_Person* person=[arr objectAtIndex:i];
        if([pre3 evaluateWithObject:person])
        {
            NSLog(@"%@",person.name);
        }
    }
    
    //占位符
    NSPredicate* preTemplate=[NSPredicate predicateWithFormat:@"name==$NAME"];
    NSDictionary* dic=[NSDictionary dictionaryWithObjectsAndKeys:@"name1",@"NAME", nil];
    NSPredicate* pre6=[preTemplate predicateWithSubstitutionVariables:dic];
    
    //快速筛选数组:
    //前面我们都是使用谓词逐个判断数组内的对象是否符合,其实数组本身有更为便捷的方法,
    //直接筛选出一个符合谓词的新数组。
    NSPredicate* pre7=[NSPredicate predicateWithFormat:@"pid>1"];
    NSMutableArray* arrPre=[arr filteredArrayUsingPredicate:pre7];
    //从一个数组中得到符合条件的新数组
    NSLog(@"%@",[[arrPre objectAtIndex:1]name]);
    
    //字符串运算符:
//    BEGINSWITH、ENDSWITH、CONTAINS 分别表示是否以某字符串开头、结尾、包含。
//    他们可以与c、d 连用,表示是否忽略大小写、是否忽略重音字母(字母上方有声调标号)。C,D都表示忽略
    NSPredicate* pre8=[NSPredicate predicateWithFormat:@"name ENDSWITH[d]'Me1'"];
    NSMutableArray* arr1=[arr filteredArrayUsingPredicate:pre8];
    NSLog(@"count :%i",arr1.count);
    //NSLog(@"arr1 :%@",[[arr1 objectAtIndex:0] name]);
    
    //SELF
    //如果数组放的都是字符串(或者是其他没有属性的类型),使用SELF。
    NSArray* arr2=[NSArray arrayWithObjects:@"apple",@"google",@"baidu", nil];
    NSPredicate* pre9=[NSPredicate predicateWithFormat:@"SELF=%@",@"apple"];
    for(i=0;i<arr2.count;i++)
    {
        if([pre9 evaluateWithObject:[arr2 objectAtIndex:i]])
        {
            NSLog(@"%i",i);
        }
    }
    NSArray* array = @[@"af",@"bg"];
    NSArray* array2 = @[@"af",@"fsd",@"bg",@"tre"];
    NSPredicate* thePredicate = [NSPredicate predicateWithFormat:@"NOT(SELF in %@)",array];
    NSArray* arr3 = [array2 filteredArrayUsingPredicate:thePredicate];
    NSLog(@"%@",arr3);
}
原文地址:https://www.cnblogs.com/shaonian/p/3022210.html