目录:

-NSPredicate
-NSSortDescriptor
-KVC

NSPredicate(谓词)

NSPredicate这个类很多人都很陌生。

这是苹果提供的快速筛选对象数据的一个过滤器,既强大又简洁。

CoreData会体现很明显。

关键字

ANDOR 这些不解释

=><>=<===!= :基本运算符

BETWEEN {x,y} :范围运算,不能使用字符串类型

IN{x,.....} :包含 字符串与数字都可用

MATCHES:正则表达式

字符串匹配:

1.LIKE :模糊查询

2.CONTAINS:包含在字符串

3.BEGINSWITH :字符串开头

4.ENDSWITH:字符串结束

5.SELF:字符串本身,在字符串数组用到。

语法格式

age == 123

多个匹配 可以加 AND OR 基本跟sql一样 如:name == 'xxx' AND age == 123

//伪代码 
Food {
name,
monry
}
@property (copy, nonatomic)NSArray <Food *>*sources;

Food)数组中变量名为name的,而它值为呵呵的对象出来。

以前的做法:

NSMutableArray *foods = [@[] mutableCopy];
for (Food *food in self.sources) {
    if ([food.name isEqualToString:@"呵呵"]) {
        [arrayM addObject:food];
    }
}

现在的做法:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@",@"呵呵"];
NSArray *foods = [self.sources filteredArrayUsingPredicate:predicate];

就会发现突然高大上了。Y(^_^)Y
当然这些还是比较基础的。。。

比较常用的:

//查询字符串匹配   [cd]:不区分大小写,只要是字符串匹配的关键字都可以用[cd] 
// *:等同于在sql的%
NSString * searchText = @"xxx";
//NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name LIKE[cd] %@",[NSString stringWithFormat:@"*%@*",searchText]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@",searchText];//等同上面写法
NSArray *searchFoods = [self.sources filteredArrayUsingPredicate:predicate];

//BETWEEN{x,y}
NSPredicate *between = [NSPredicate predicateWithFormat:@"monry BETWEEN{10,100}"];
NSArray *betweenFoods = [self.sources filteredArrayUsingPredicate: between];

//IN{x,.....}
NSPredicate *inPred = [NSPredicate predicateWithFormat:@"name IN{%@,%@}",@"xx",@"xxxx"];
NSArray * inPredFoods = [self.sources filteredArrayUsingPredicate: inPred];

//MATCHES 正则
NSString *regex =@"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";//手机号
NSPredicate *regexTestMobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL isPhone = [regexTestMobile evaluateWithObject:@"13111111111"];

NSSortDescriptor(排序)

排序方面就不写老做法了。(就是懒*\(^o^)/*)

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"monry" ascending:YES];
NSArray *foods = [self.sources sortedArrayUsingDescriptors:@[sortDescriptor]];
/*
//这也是比较常用的用法,这里做简单的判断,等同于上面写法
[self.sources sortUsingComparator:^NSComparisonResult(Food * _Nonnull obj1, Food *_Nonnull obj2) {
    return !sender.selectedSegmentIndex?[obj1.monry compare:obj2.monry]:[obj2.monry compare:obj1.monry];
}];
*/

当然这个只是简单排序。

复杂一点需要自己写逻辑也可以使用NSArrayNSSet里面提供方法的。

@interface NSArray<ObjectType> (NSExtendedArray)
...
- (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (*)(ObjectType, ObjectType, void * __nullable))comparator context:(nullable void *)context;
- (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (*)(ObjectType, ObjectType, void * __nullable))comparator context:(nullable void *)context hint:(nullable NSData *)hint;
- (NSArray<ObjectType> *)sortedArrayUsingSelector:(SEL)comparator;
...
- (NSArray<ObjectType> *)sortedArrayUsingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);
- (NSArray<ObjectType> *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);
@end
@interface NSMutableArray<ObjectType> (NSExtendedMutableArray)
...
- (void)sortUsingFunction:(NSInteger (*)(ObjectType,  ObjectType, void * __nullable))compare context:(nullable void *)context;
- (void)sortUsingSelector:(SEL)comparator;
...
- (void)sortUsingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);
- (void)sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);
@end

还有些这里就不占字数了,自己找去( ´ ▽ ` )ノ。

使用 KVC 计算

关键字

@min:最小值

@max :最大值

@avg :平均值

@sum:总和

基础运算
NSInteger min = [[self.sources valueForKeyPath:@"@min.monry"] integerValue];
NSInteger min = [[self.sources valueForKeyPath:@"@max.monry"] integerValue] ;
NSInteger sum =[[self.sources valueForKeyPath:@"@sum.monry"] integerValue];
double avg = [[self.sources valueForKeyPath:@"@avg.monry"] doubleValue] ;

使用后瞬间觉得世界美好了很多。

总结:

当然这些都是看项目里面逻辑需求,如何搭配看个人了(☆_☆)。
最简单的实战经验:我想计算我本地未读消息的总和,然后显示到Tabbar Badge 或者显示到App Icon Badge 里面去。

逻辑:使用NSPredicate筛选出未读消息会话,然后@sum.count,最后赋值。
当然也要看需求,还有你实现逻辑,来取决你的计算。

DEMO

最后,上面有哪些写的不对可以留言吐槽,非常欢迎共同学习的人。