目錄:
-NSPredicate
-NSSortDescriptor
-KVC
NSPredicate(謂詞)
NSPredicate這個類很多人都很陌生。
這是蘋果提供的快速篩選對象數(shù)據(jù)的一個過濾器,既強大又簡潔。
在CoreData會體現(xiàn)很明顯。
關鍵字
AND OR 這些不解釋
= > < >= <= == != :基本運算符
BETWEEN {x,y} :范圍運算,不能使用字符串類型
IN{x,.....} :包含 字符串與數(shù)字都可用
MATCHES:正則表達式
字符串匹配:
1.LIKE :模糊查詢
2.CONTAINS:包含在字符串
3.BEGINSWITH :字符串開頭
4.ENDSWITH:字符串結束
5.SELF:字符串本身,在字符串數(shù)組用到。
語法格式
對象字段 匹配關鍵字 匹配字段 如: age == 123
多個匹配 可以加 AND OR 基本跟sql一樣 如:name == 'xxx' AND age == 123
//偽代碼
Food {
name,
monry
}
@property (copy, nonatomic)NSArray <Food *>*sources;
例如:我要篩選對象(Food)數(shù)組中變量名為name的,而它值為呵呵的對象出來。
以前的做法:
NSMutableArray *foods = [@[] mutableCopy];
for (Food *food in self.sources) {
if ([food.name isEqualToString:@"呵呵"]) {
[arrayM addObject:food];
}
}
現(xiàn)在的做法:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@",@"呵呵"];
NSArray *foods = [self.sources filteredArrayUsingPredicate:predicate];
就會發(fā)現(xiàn)突然高大上了。Y(_)Y
當然這些還是比較基礎的。。。
比較常用的:
//查詢字符串匹配 [cd]:不區(qū)分大小寫,只要是字符串匹配的關鍵字都可以用[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];
}];
*/
當然這個只是簡單排序。
復雜一點需要自己寫邏輯也可以使用NSArray或NSSet里面提供方法的。
@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
還有些這里就不占字數(shù)了,自己找去( ′ ▽ ` )?。
使用 KVC 計算
關鍵字
@min:最小值
@max :最大值
@avg :平均值
@sum:總和
基礎運算
NSInteger min = [[self.sources valueForKeyPath:@"@min.monry"] integerValue];
NSInteger max = [[self.sources valueForKeyPath:@"@max.monry"] integerValue] ;
NSInteger sum =[[self.sources valueForKeyPath:@"@sum.monry"] integerValue];
double avg = [[self.sources valueForKeyPath:@"@avg.monry"] doubleValue] ;
使用后瞬間覺得世界美好了很多。
總結:
當然這些都是看項目里面邏輯需求,如何搭配看個人了(☆_☆)。
最簡單的實戰(zhàn)經(jīng)驗:我想計算我本地未讀消息的總和,然后顯示到Tabbar Badge 或者顯示到App Icon Badge 里面去。
邏輯:使用NSPredicate篩選出未讀消息會話,然后@sum.count,最后賦值。
當然也要看需求,還有你實現(xiàn)邏輯,來取決你的計算。
最后,上面有哪些寫的不對可以留言吐槽,非常歡迎共同學習的人。