ios NSPredicate 谓词过滤

// NSPredicate :是Foundation框架提供的用来指定过滤条件的类。该类主要用于指定过滤器的条件,该对象可以准确的描述所需条件,对每个对象通过谓词进行筛选,判断是否与条件相匹配。

// 定义谓词对象,谓词对象中包含了过滤条件
// 1)、过滤对象是数组:使用- (void)filterUsingPredicate:(NSPredicate )predicate; 针对可变数组进行过滤,过滤掉可变数组中不符合条件的。-
//
 (NSArray )filteredArrayUsingPredicate:(NSPredicate )predicate;

  针对不可变数组进行过滤,将符合条件的元素组成一个新数组进行返回
//
// 2)、对单个对象进行判断过滤使用:
- (BOOL)evaluateWithObject:(id)object;

向谓词对象发送该方法,参数是过滤的对象。常见于和正则表达式配合使用。
//

// NSPredicate predicate = [NSPredicate predicateWithFormat:@"age<%d",30];

// 1)数组
// 使用谓词条件过滤数组中的元素,过滤之后返回查询的结果
NSArray filterArray = [persons filteredArrayUsingPredicate:predicate];
NSLog(@"filterArray=%@",filterArray);
// 2)单个对象
// 用来承接过滤出来的对象
// NSMutableArray matchObjArray = [NSMutableArray array];
// for (Person item in persons) {

// A、判断是否满足条件
// 判断语句:evaluateWithObject,符合过滤条件就返回yes
// if ([predicate evaluateWithObject:item]) {
// [matchObjArray addObject:item];
// }
// NSLog(@"符合过滤条件的对象是:%@", matchObjArray);
//
// 逻辑运算符:AND、OR、NOT (&& || !)
// 这几个运算符计算与、或、非的结果。
//
// 2.使用&&符号 (也可以使用and)
// NSPredicate predicate = [NSPredicate predicateWithFormat:@"name>='小白' and age>20"];
// filterArray(persons,predicate);


// 关系操作
// ANY,SOME:指定下列表达式中的任意元素。比如,ANY person.age < 18。
//
ALL:指定下列表达式中的所有元素。比如,ALL person.age < 18。
// NONE:指定下列表达式中没有的元素。比如,NONE person.age < 18。它在逻辑上等于NOT (ANY ...)。
//
IN:等于SQL的IN操作,左边的表达必须出现在右边指定的集合中。比如,name IN { '小黑','Gose'}
//
// 这个并非iOS里面的self那个保留字。为什么会引入SELF这个东西呢?是因为不可能每次写谓词都有那么明确的键路径好用,而这个SELF可以引用用于谓词计算的对象。

// 3.IN(包含)
// NSPredicate predicate = [NSPredicate
// predicateWithFormat:@"self.name IN {%@,%@} || self.age IN {30,50}",@"小黑",@"Gose"];
// filterArray(persons,predicate);
// 
// 4.以...开头 BEGINSWITH
// NSPredicate predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];
// filterArray(persons,predicate);


// 5.以...结尾 ENDSWITH
// NSPredicate predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'se'"];
// filterArray(persons,predicate);

// 6. 包含..字符 CONTAINS 与c、d 连用,表示是否忽略大小写、是否忽略重音字母(字母上方有声调标号)。
// NSPredicate predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];
// filterArray(persons,predicate);
//
// NSPredicate predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[c] 'a'"];
// filterArray(persons,predicate);

// 7.like :匹配任意多个字符 ?:表示一个字符

// a : 以a结尾的
//
a : 字符串中含有a字符的

// ?a : 第二个字符为a的

#person.h

<span style="font-size:18px;">#import <Foundation/Foundation.h>

@interface Person : NSObject

@property(nonatomic,copy)NSString *name;
@property(nonatomic,assign)NSUInteger age;

+ (instancetype)personWithName:(NSString *)name andAge:(NSUInteger)age;

@end</span>
#person.m

<span style="font-size:18px;">#import "Person.h"

@implementation Person

-(instancetype)initWithName:(NSString *)name andAge:(NSUInteger)age{
    self = [super init];
    if (self) {
        self.name = name;
        self.age = age;
    }
    return self;
}

+ (instancetype)personWithName:(NSString *)name andAge:(NSUInteger)age {
    Person *person = [[Person alloc] initWithName:name andAge:age];
    return person;
}

- (NSString *)description {
    NSString *string = [NSString stringWithFormat:@"name=%@,age=%ld",_name,_age];
    return string;
}

@end</span>
#main.m

#import <Foundation/Foundation.h>
#import "Person.h"

void filterArray(NSArray *array,NSPredicate *predicate) {
    NSArray *filterArray = [array filteredArrayUsingPredicate:predicate];
    NSLog(@"filterArray=%@",filterArray);

}
int main(int argc, const char * argv[]) {
    //person的数组
    NSArray *persons = [NSArray arrayWithObjects:
                        [Person personWithName:@"mac" andAge:20],
                        [Person personWithName:@"hate" andAge:25],
                        [Person personWithName:@"999ere" andAge:30],
                        [Person personWithName:@"Aruse" andAge:28],
                        [Person personWithName:@"Erse" andAge:30],
                        [Person personWithName:@"Ahon" andAge:29],
                        [Person personWithName:@"Gose" andAge:30],
                        [Person personWithName:@"ate" andAge:20],
                        [Person personWithName:@"Iog" andAge:30],
                        [Person personWithName:@"小白" andAge:50],
                        [Person personWithName:@"小黑" andAge:30],
                        nil];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like '?a*'"];
    filterArray(persons,predicate);


    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值